简体   繁体   English

while循环无法正常运行

[英]while loop not functioning correctly

Essentially I need this code I've written in JES to keep asking the user regardless of how many times and what combination (which is clearly an issue at the moment) and only exit if the number entered is between 0 and 9. At the moment if I enter -3 and then enter 50 it exits. 本质上,我需要用JES编写的这段代码来不断询问用户,无论它发生了多少次和使用哪种组合(目前这显然是一个问题),并且仅在输入的数字介于0到9之间时才退出。如果我输入-3,然后输入50,则退出。 I'm not sure whether I'm close or very far off. 我不确定我离得很近还是很远。

Cheers. 干杯。

def main()
userInput = requestInteger("Enter a number between 0 and 9...")
  while userInput < int(0) :
    printNow("Error! Inputs must be between 0 and 9. Please re-enter...")
    userInput = requestInteger("Enter a number between 0 and 9...")
  while userInput > 9:
    printNow("Error! Inputs must be between 0 and 9. Please re-enter...")
    userInput = requestInteger("Enter a number between 0 and 9...")
  print "Thanks, you have entered: ",userInput,

The code you've written is working exactly as expected... if you want to keep asking the user for input, you'll want to use a while True and break out of it when correct input is given: 您编写的代码完全按预期运行...如果您想继续要求用户输入,您将需要使用while True一段while True ,并在给出正确输入后break它:

while True:
    user_input = requestInteger("Enter a number between 0 and 9...")

    if not 0 <= user_input <= 9:
        printNow("Error! Input must be between 0 and 9. Please re-enter.")
    else:
        print "Thanks, you've entered: ", user_input
        break

def main(): def main():

    user_input = int(raw_input("Enter a number between 0 and 9..."))

    while user_input not in range(0, 10):
            print("Error! Inputs must be between 0 and 9. Please re-enter...")
            user_input = int(raw_input("Enter a number between 0 and 9..."))
    else:
            print "Thanks, you have entered: ",user_input

main() 主要()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM