分享
三行代码  ›  专栏  ›  技术社区  ›  martin2137

while循环语句的输入验证 - Input Validation with While Loop Statement

  •  -1
  • martin2137  · 技术社区  · 1 周前

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

    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): "))
    

    应提升用户以重新输入速度,直到其满足指定的条件。在本例中,用户需要输入 51 60 在他们冲出 while 循环。

    3 回复  |  直到 1 周前
        1
  •  0
  •   Mason Caiby    1 周前

    你没有正确核对号码。

    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): "))
    
        2
  •  0
  •   Dipen Bakraniya nabin    1 周前

    练习在括号中写入条件并分别检查多个条件。

    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): ")) 
    
        3
  •  0
  •   Dipen Bakraniya nabin    1 周前

    请尝试以下代码:

    current_speed = int(input("What is the current speed (in mph 51 - 60)? "))
    
    while current_speed > 60 or current_speed < 51:
        current_speed = int(input("Please enter a speed between (51 - 60 mph): "))