我是一个相当新的程序员,目前我已经被分配了一个项目来创建一个循环计算器,即使你使用其中一个计算器函数,它仍将继续运行,直到你提供选择。我的计算器目前的问题是,每当我尝试在Pydev上测试我的函数时,都会有一个不间断的终止。我已经解决了Pydev将我的一个变量(选项)读取为未使用的第一个问题,因此我使用了" ctrl + 1"伎俩和摆脱未使用的变量警告。关于我如何能够运行该程序并让它不断循环的任何提示?
def main():
loop=1
choice=0 # @UnusedVariable
while loop == 1:
print("Welcome to Calculator Function!")
print("Your options are:")
print(" ")
print("1/) Addition")
print("2/) Subtraction")
print("3/) Multiplication")
print("4/) Division")
print("5/) Quit CalculatorFunction.py")
print(" ")
choice = int(raw_input("Choose your option: ").strip())
if choice == 1:
add1 = input("Add what: ")
add2 = input("To what: ")
print add1, "+", add2, "=", add1+add2
elif choice == 2:
sub2 = input("Subtract what: ")
sub1 = input("From what: ")
print sub1, "-", sub2, "=", sub1-sub2
elif choice == 3:
mult1 = input("Multiply what: ")
mult2 = input("To what: ")
print mult1, "*", mult2, "=", mult1*mult2
elif choice == 4:
div2= input("Divide what: ")
div1= input("From what: ")
print div1, "/", div2, "=", div1/div2
elif choice == 5:
loop = 0
print"Thank you for using CalculatorFunction.py have a good day!"
else:
print"No selection made, please try again."
if __name__ == '__main__':
main()
答案 0 :(得分:0)
您的if - else
块在while循环之外,这就是它不起作用的原因。这是正确缩进的代码:
def main():
loop=1
choice=0 # @UnusedVariable
while loop == 1:
print("Welcome to Calculator Function!")
print("Your options are:")
print(" ")
print("1/) Addition")
print("2/) Subtraction")
print("3/) Multiplication")
print("4/) Division")
print("5/) Quit CalculatorFunction.py")
print(" ")
choice = int(raw_input("Choose your option: ").strip())
if choice == 1:
add1 = input("Add what: ")
add2 = input("To what: ")
print add1, "+", add2, "=", add1+add2
elif choice == 2:
sub2 = input("Subtract what: ")
sub1 = input("From what: ")
print sub1, "-", sub2, "=", sub1-sub2
elif choice == 3:
mult1 = input("Multiply what: ")
mult2 = input("To what: ")
print mult1, "*", mult2, "=", mult1*mult2
elif choice == 4:
div2= input("Divide what: ")
div1= input("From what: ")
print div1, "/", div2, "=", div1/div2
elif choice == 5:
loop = 0
print"Thank you for using CalculatorFunction.py have a good day!"
else:
print"No selection made, please try again."
if __name__ == '__main__':
main()
这在pydev上运作良好。
答案 1 :(得分:0)
感谢您关于“缩进”的提示和其他一些事情,我终于想通了这个程序,感谢大家的意见。我做了什么来解决我的立即终止是修复缩进。然后为了使程序不断循环,如果没有做出选择我添加了继续,实质上终止程序的唯一方法是选择列出为“5”的选项quit。
def main()
loop=1
choice=0 # @UnusedVariable
while loop == 1:
print(" ")
print("Welcome to Calculator Function!")
print("Your options are:")
print(" ")
print("1) Addition")
print("2) Subtraction")
print("3) Multiplication")
print("4) Division")
print("5) Quit CalculatorFunction.py")
print(" ")
choice = int(raw_input("Choose your option: ").strip())
if choice == 1:
add1 = int(raw_input("Add what: "))
add2 = int(raw_input("To what: "))
print add1, "+", add2, "=", add1+add2
elif choice == 2:
sub2 = int(raw_input("Subtract what: "))
sub1 = int(raw_input("From what: "))
print sub1, "-", sub2, "=", sub1-sub2
elif choice == 3:
mult1 = int(raw_input("Multiply what: "))
mult2 = int(raw_input("To what: "))
print mult1, "*", mult2, "=", mult1*mult2
elif choice == 4:
div2= int(raw_input("Divide what: "))
div1= int(raw_input("From what: "))
print div1, "/", div2, "=", div1/div2
elif choice == 5:
loop = 0 #Ends the program
print"Thank you for using CalculatorFunction.py have a good day!"
else:
print"No selection made, please try again."
continue #loops the program
如果名称 =='主要': main()