Python:我陷入了无限循环

时间:2015-08-26 11:02:13

标签: python while-loop infinite-loop

问题如下:

Example output:

Calculator
Give the first number: 100
Give the second number: 25
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 100 25
Please select something (1-6): 5
Give the first number: 10
Give the second number: 30
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 10 30
Please select something (1-6): 1
The result is: 40
(1) +
(2) -
(3) *
(4) /
(5)Change numbers
(6)Quit
Current numbers: 10 30
Please select something (1-6): 6
Thank you!

我的解决方案是:

print("Calculator")

var1 = input("Give the first number: ")
var2 = input("Give the second number: ")

print("(1) +\n(2) -\n(3) *\n(4) /\n(5)Change numbers\n(6)Quit")
print("Current numbers: " + var1 + " " + var2)

opcion = input("Please select something (1-6): ")

while (opcion != "6"):
    if (opcion == "1"):
        var3 = int(var1) + int(var2)
        print("The result is: " + str(var3))
    elif (opcion == "2"):
        var3 = int(var1) - int(var2)
        print("The result is: " + str(var3))
    elif (opcion == "3"):
        var3 = int(var1) * int(var2)
        print("The result is: " + str(var3))
    elif (opcion == "4"):
        var3 = int(var1) / int(var2)
        print("The result is: " + str(var3))
    elif (opcion == "5"):
        var1 = input("Give the first number: ")
        var2 = input("Give the second number: ")
        opcion = input("Please select something (1-6): ")
    else:
        print("Selection was not correct.")

print("Thank you!")

但是在“elif(opcion ==”5“)中:”自动校正器声明我陷入无限循环,我在输入两个新数字后尝试了休息,但它完全退出了while循环, 有任何想法吗?谢谢。

1 个答案:

答案 0 :(得分:6)

代码中的问题:

你只能在开始时获得opcion,然后只有当opcion为5时才会获得opcion

当你给opcion =“1”时会发生什么,它会添加并给你答案,然后检查while语句什么时候为真,然后再次添加并再次检查goes oooooooooooon以便产生无限循环

<强>修饰:

print("Calculator")

var1 = input("Give the first number: ")
var2 = input("Give the second number: ")

print("(1) +\n(2) -\n(3) *\n(4) /\n(5)Change numbers\n(6)Quit")
print("Current numbers: " + var1 + " " + var2)

while True:
    opcion = input("Please select something (1-6): ")
    if (opcion == "1"):
        var3 = int(var1) + int(var2)
        print("The result is: " + str(var3))
    elif (opcion == "2"):
        var3 = int(var1) - int(var2)
        print("The result is: " + str(var3))
    elif (opcion == "3"):
        var3 = int(var1) * int(var2)
        print("The result is: " + str(var3))
    elif (opcion == "4"):
        var3 = int(var1) / int(var2)
        print("The result is: " + str(var3))
    elif (opcion == "5"):
        var1 = input("Give the first number: ")
        var2 = input("Give the second number: ")
    elif (opcion == "6"):
        break
    else:
        print("Selection was not correct.")

print("Thank you!")

更改进的解决方案:

import operator
print("Calculator")

var1 = input("Give the first number: ")
var2 = input("Give the second number: ")

print("(1) +\n(2) -\n(3) *\n(4) /\n(5)Change numbers\n(6)Quit")
print("Current numbers: " + var1 + " " + var2)
operations={"1":operator.add,"2":operator.sub,"3":operator.mul,"4":operator.div}
while True:
    option = input("Please select something (1-6): ")
    print option
    if option in operations:
        var3=operations[option](int(var1),int(var2))        
        print "value"+str( var3)
        continue
    if (option == "6"):
        print "quiting"
        break
    elif (option == "5"):
        var1 = input("Give the first number: ")
        var2 = input("Give the second number: ")
    else:
        print("Selection was not correct.")

print("Thank you!")

注意:

它应该是options而不是opcion正确的英语术语,但它确实无关紧要:P