我的代码遇到问题。每当我从头开始尝试使用python 3时,我最终会遇到无限循环。我不确定我做错了什么。这是我想要开始工作的计算器......
loop = 1 # 1 means loop; anything else means don't loop.
choice = 0 # This variable holds the user's choice in the menu
while loop == 1:
# Print what options you have
print ("Welcome to StandardCalculator.py")
print ("your options are:")
print (" ")
print ("1) Addition")
print ("2) Subtraction")
print ("3) Multiplication")
print ("4) Division")
print ("5) Raise to the Power")
print ("6) Quit StandardCalculator.py")
print (" ")
choice = input("Enter choice(1/2/3/4):")
if choice == 1:
add1 = input("Add this: ")
add2 = input("to this: ")
print (add1, "+", add2, "=", add1 + add2)
elif choice == 2:
sub2 = input("Subtract this: ")
sub1 = input("from this: ")
print (sub1, "-", sub2, "=", sub1 - sub2)
elif choice == 3:
mul1 = input("Multiply this: ")
mul2 = input("with this: ")
print (mul1, "*", mul2, "=", mul1 * mul2)
elif choice == 4:
div1 = input("Divide this: ")
div2 = input("by this: ")
print (div1, "/", div2, "=", div1 / div2)
elif choice == 5:
pow1 = input ("Raise this: ")
pow2 = input ("by: ")
print (pow1, "**", pow2, "=", pow1 ** pow2)
elif choice == 6:
loop = 0
print ("Thank-you for using StandardCalculator.py!")
答案 0 :(得分:0)
这里的问题最终是缩进。目前,您的while循环只会继续打印您的提示,绝不允许您输入选择或将逻辑应用于该选项。
此外,当您获得choice
输入时,它是String格式,您将它与int进行比较。让我们假设输入有效,您有两个选择:
int
那个。
choice = int(choice)
与字符串比较
if choice == "1":
...
elif choice == "2":
...
修复缩进和类型问题后,请查看AndreL的答案。
loop = 1 # 1 means loop; anything else means don't loop.
choice = 0 # This variable holds the user's choice in the menu
while loop == 1:
# Print what options you have
print ("Welcome to StandardCalculator.py")
print ("your options are:")
print (" ")
print ("1) Addition")
print ("2) Subtraction")
print ("3) Multiplication")
print ("4) Division")
print ("5) Raise to the Power")
print ("6) Quit StandardCalculator.py")
print (" ")
choice = input("Enter choice(1/2/3/4):")
choice = int(choice)
if choice == 1:
add1 = input("Add this: ")
add2 = input("to this: ")
print (add1, "+", add2, "=", add1 + add2)
elif choice == 2:
sub2 = input("Subtract this: ")
sub1 = input("from this: ")
print (sub1, "-", sub2, "=", sub1 - sub2)
elif choice == 3:
mul1 = input("Multiply this: ")
mul2 = input("with this: ")
print (mul1, "*", mul2, "=", mul1 * mul2)
elif choice == 4:
div1 = input("Divide this: ")
div2 = input("by this: ")
print (div1, "/", div2, "=", div1 / div2)
elif choice == 5:
pow1 = input ("Raise this: ")
pow2 = input ("by: ")
print (pow1, "**", pow2, "=", pow1 ** pow2)
elif choice == 6:
loop = 0
print ("Thank-you for using StandardCalculator.py!")
答案 1 :(得分:0)
您尚未将选择部分包含在循环中,请在print (" ")
之后标记所有内容,以便将其包含在while循环中。
同样将if语句从if choice == 1
更改为if choice == "1"
选项将是字符串而不是int。
最后,当你得到答案时,你需要将字符串输入更改为整数,然后更改
print (add1, "+", add2, "=", add1 + add2)
成像:
print (add1, "+", add2, "=", int(add1) + int(add2))
如果你懒得像我一样,如果用户输入了无法转换为字符串的内容,你的程序就会崩溃,所以你也必须在那里加入try
。
答案 2 :(得分:0)
编辑:误读了问题。
您不会更改'循环'的值,因此它总是等于1.我认为这就是您的目标:
while loop == 1:
# Print what options you have
print ("Welcome to StandardCalculator.py")
print ("your options are:")
print (" ")
print ("1) Addition")
print ("2) Subtraction")
print ("3) Multiplication")
print ("4) Division")
print ("5) Raise to the Power")
print ("6) Quit StandardCalculator.py")
print (" ")
choice = input("Enter choice(1/2/3/4):")
if choice == 1:
add1 = input("Add this: ")
add2 = input("to this: ")
print (add1, "+", add2, "=", add1 + add2)
elif choice == 2:
sub2 = input("Subtract this: ")
sub1 = input("from this: ")
print (sub1, "-", sub2, "=", sub1 - sub2)
elif choice == 3:
mul1 = input("Multiply this: ")
mul2 = input("with this: ")
print (mul1, "*", mul2, "=", mul1 * mul2)
elif choice == 4:
div1 = input("Divide this: ")
div2 = input("by this: ")
print (div1, "/", div2, "=", div1 / div2)
elif choice == 5:
pow1 = input ("Raise this: ")
pow2 = input ("by: ")
print (pow1, "**", pow2, "=", pow1 ** pow2)
elif choice == 6:
loop = 0
else:
loop = 1;
print ("Thank-you for using StandardCalculator.py!")
请注意,我添加了一个' else'将循环设置为1.这样,如果有人做出无效选择,它将不会中断,而是会再次继续循环。
小心缩进,你的代码被卡在打印六个选项上,因为循环最初只包含print语句。你的缩进并没有包含允许中断条件的其余代码。
while loop == 1:
# Print what options you have
print ("Welcome to StandardCalculator.py")
print ("your options are:")
print (" ")
print ("1) Addition")
print ("2) Subtraction")
print ("3) Multiplication")
print ("4) Division")
print ("5) Raise to the Power")
print ("6) Quit StandardCalculator.py")
print (" ") # <- End of loop
choice = input("Enter choice(1/2/3/4):") # <- Outside loop, needed to be indented.
最后,在确定你的代码运行正常后,但我在你的else / if中删除了一些缩进,理想情况下Python应缩进4个空格。
理想:
if choice == 1:
add1 = input("Add this: ")
add2 = input("to this: ")
print (add1, "+", add2, "=", add1 + add2)
loop = 0
不理想:
if choice == 1:
add1 = input("Add this: ")
add2 = input("to this: ")
print (add1, "+", add2, "=", add1 + add2)
loop = 0