使用While循环语句进行输入验证

时间:2019-09-10 18:13:54

标签: python while-loop

我在检查用户输入时遇到问题。如果它不在特定范围内,则它将再次提示用户并澄清参数范围。 我很好奇下面的代码为什么不起作用。

current_speed = int(input("What is the current speed (in mph 51 - 60)? "))

while 51 > current_speed > 60:
    current_speed = int(input("Please enter a speed between (51 - 60 mph): "))

应该提升用户重新输入速度,直到它达到指定的条件。在此示例中,用户需要输入5160 mph之间的速度,然后才能脱离while循环。

3 个答案:

答案 0 :(得分:0)

您没有正确检查数字。

current_speed = int(input("What is the current speed (in mph 51 - 60)? "))

while 51 > current_speed or current_speed > 60: 
    current_speed = int(input("Please enter a speed between (51 - 60 mph): "))

答案 1 :(得分:0)

尝试以下代码:

printSection

答案 2 :(得分:0)

具有在括号中编写条件并分别检查多个条件的习惯。

current_speed = int(input("What is the current speed (in mph 51 - 60)? "))

while (current_speed < 51 or current_speed > 60):
    current_speed = int(input("Please enter a speed between (51 - 60 mph): "))