用户使用while语句输入整数

时间:2016-10-03 12:50:49

标签: python

确定需要理解为什么我的代码不起作用。 目前,在输入错误的引脚后,它会陷入无限循环,要求用户输入引脚。

我相信我在某种程度上搞砸了while声明。

def inputValidator():
    userInput = requestInteger("Please input a PIN between 1 - 1000")
    while 1 > userInput > 1000 : 
        requestInteger("Your PIN is not within 1 - 1000, please try again")
    print("Thanks! your new PIN is " + str(userInput))

感谢帮助人员!

3 个答案:

答案 0 :(得分:2)

试试这个:

def inputValidator():
    userInput = requestInteger("Please input a PIN between 1 - 1000")
    while userInput<1 or userInput>1000:
        userInput = requestInteger("Your PIN is not within 1 - 1000, please try again")
    print("Thanks! your new PIN is " + str(userInput))

如果userInput小于1或大于1000,您需要来自用户的新输入 - 并且像@Polina F.所说 - 您没有为userInput分配新值在while循环中。这就是它永远循环的原因。

答案 1 :(得分:1)

您不会在while循环中分配任何内容。 userInput永远不会更新 - 因此您无法退出循环

答案 2 :(得分:1)

您没有将requestInteger分配给userInput

while 1 > userInput > 1000 : 
  userInput =requestInteger("Your PIN is not within 1 - 1000, please try again")
相关问题