str.isdigit()导致程序挂起

时间:2019-05-13 21:00:40

标签: python

我正在尝试使用str.isdigit()来尝试确定用户输入。但是,当用户在所需参数之间输入数字值时,代码将挂起。在参数之外输入其他输入后,代码将正常运行。

代码是:

Input_Value = input("Please choose which activity would would like to run:  ")

while True:

    if Input_Value == "8" or Input_Value == "9" or Input_Value == "0":
        Input_Value = input("Please input an 'x' or a number between 1 & 7: ")

    elif len(Input_Value) > 1:
        Input_Value = input("Please input an 'x' or a number between 1 & 7: ")

    elif (Input_Value.isdigit()) == True:
        Choice = int(Input_Value)
        continue

    elif Input_Value == "x" or Input_Value == "X":
        print()
        print("Thank you for taking part. Good bye")
        print()
        exit()

    else:
        Input_Value = input("Please input an 'x' or a number between 1 & 7: ")

if Choice == 1:
    # do something here

elif Choice == 2:

在我放入elif (input_Value.isdigit() == True:之前,没有出现错误,并且代码运行正常(没有捕获错误)。

2 个答案:

答案 0 :(得分:4)

放在elif分支中的continue导致while True循环的下一次迭代被执行。由于nothin已更改,因此将再次选择elif分支,continue将跳至下一个迭代... 永远重复,不允许执行任何其他代码。

您可能正在寻找break

答案 1 :(得分:1)

您将input放在循环外,并且在无限循环内有一个继续。因此,Input_Value的值永远不会改变,并且总是一遍又一遍地重复做同样的事情...

由于在循环之外也有代码,因此建议您尝试使用break而不是continue

您可能还想尝试使用:

if Input_Value in ["8", "9", "0"]:
相关问题