如何将类型检查功能作为while循环?

时间:2015-03-13 21:27:11

标签: python loops while-loop typechecking

Ptarget = int(input("What is your target amount of points to achieve?"))
while Ptarget != int:
    print("You have not provided a valid input, please try again.")
    Ptarget = int(input("What is your target amount of points to achieve?"))

如何使while循环起作用,通过在不破坏程序的情况下要求用户输入另一个输入(如果前一个不是整数)?

1 个答案:

答案 0 :(得分:0)

使用例外

while True:
    try:
        Ptarget = int(input("What is your target amount of points to achieve?"))
        break
    except:
        print("You have not provided a valid input, please try again.")