Python:我陷入了无限循环 [英] Python: I fell in an infinite loop

查看:126
本文介绍了Python:我陷入了无限循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题如下:

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循环中退出, 有任何想法吗?谢谢.

But in "elif (opcion == "5"):" the automatic corrector states that I fell in an infinite loop, I tried a break after the two new numbers are entered, but it completely exits from the while-loop, any ideas? Thank you.

推荐答案

您的代码中的问题:

您只能在while循环之外分配opcion,然后仅在opcion == "5"时修改opcion.

You only assign opcion outside the while loop and after which you are only modifying opcion when opcion == "5".

给出opcion = "1"时会发生什么,它会添加并给出答案,然后检查while语句when为true,因为opcion不变.然后,它再次添​​加,并再次检查,然后按goes oooooooooooon,从而产生无限循环.

What happens when you give opcion = "1" is it adds and gives you the answer and after which checks the while statement when is true, because opcion did not change. It then adds again, and checks again, and goes oooooooooooon, so an infinite loop is produced.

修改:

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

It should be options and not opcion in correct English terms but it really doesn't matter :P

这篇关于Python:我陷入了无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆