while循环不正常

时间:2015-01-10 01:53:29

标签: python

基本上我需要这些我在JES中编写的代码,以便不断询问用户多少次和什么组合(这显然是目前的问题),并且只有在输入的数字介于0和9之间时才退出。此刻,如果我输入-3然后输入50则退出。我不确定我是近距离还是远距离。

干杯。

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,

2 个答案:

答案 0 :(得分:1)

您编写的代码完全符合预期......如果您想继续询问用户输入内容,您会想要使用while Truebreak在给出正确输入时将其取出:

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

答案 1 :(得分:0)

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()的