-2

I am new to python and have faced this issue. What this program does is ask the user his or her age. The age has to be a number or else it will return a value error. I have used the try/except method to respond to the error, but i also want it so that the age the user enters is below a specific value(eg 200).

while True:
    try:
        age=int(input('Please enter your age in years'))
        break
    except ValueError:
        print ('\n\n\n\nThat\'s not a valid Number.\nPlease try Again.\n\n\n\n')
    except:
        if age>=200:
            print ('\n\n\n\nThat\'s not a valid Number.\nPlease try Again.\n\n\n\n')

print (f'Your age is {age}')

I tried this and a bunch of other stuff. Can anyone help?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Shell1500
  • 330
  • 1
  • 5
  • 14
  • 1
    you don't need the `break` – Paul H Jul 05 '18 at 06:58
  • 1
    What is your question? – DYZ Jul 05 '18 at 06:59
  • 1
    @PaulH Actually, they _do_ need a break. Otherwise, how does the loop terminate? – DYZ Jul 05 '18 at 07:00
  • @DyZ either the print statement needs to be inside the loop, or you don't need a loop at all. – Paul H Jul 05 '18 at 07:02
  • 1
    @PaulH They want to loop _until_ the user enters a correct age, and then print the entered age. The `if` statement is broke, but the rest of the logic is correct. – DYZ Jul 05 '18 at 07:03
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Mr. T Jul 05 '18 at 08:37

3 Answers3

2

You first need to check if value entered is an integer, do this in try clause. You then need to check if the value is within the range, do this in the else clause which only gets executed if the try block is successful.
Break out if the value is within the range. Below code shows this.

while True:
    try:
        age=int(input('Please enter your age in years'))

    except ValueError:
        print ('\n\n\n\nThat\'s not a valid Number.\nPlease try Again.\n\n\n\n')
    else:
        if age>=200:
            print ('\n\n\n\nThat\'s not a valid Number.\nPlease try Again.\n\n\n\n')
        else:
            break



print (f'Your age is {age}')
hsnsd
  • 1,728
  • 12
  • 30
1

A possible solution:

while True:
    age_input = input("Please enter your age in years: ")

    # Check both if the string is an integer, and if the age is below 200.
    if age_input.isdigit() and int(age_input) < 200:
        print("Your age is {}".format(age_input))
        break

    # If reach here, it means that the above if statement evaluated to False.
    print ("That's not a valid Number.\nPlease try Again.")

You don't need to do exception handling in this case.

isdigit() is a method of a String object that tells you if the given String contains only digits.

Rafael
  • 7,002
  • 5
  • 43
  • 52
0

You can do an if statement right after the input(), still keeping the except ValueError, example:

while True:
    try:
        age=int(input('Please enter your age in years'))
        if age < 200:
            # age is valid, so we can break the loop
            break
        else:
            # age is not valid, print error, and continue the loop
            print('\n\n\n\nThat\'s not a valid Number.\nPlease try Again.\n\n\n\n')
    except ValueError:
        print('\n\n\n\nThat\'s not a valid Number.\nPlease try Again.\n\n\n\n')  
Edwin van Mierlo
  • 2,398
  • 1
  • 10
  • 19