0

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. 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,
1
  • Your code is missing a : after the function name. Please make sure you paste your original code :) Commented Jan 10, 2015 at 1:57

2 Answers 2

1

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:
    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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.