0

Loop with min/max input

Hello, I am trying to create a loop for age input. I'd like to have a max of 13, as example and min of 10 years. I have tried several things such as While True: .. etc and I keep getting it wrong. What I'm trying, is a loop that will break if the input is not between 10 and 13, so it will come up until the input is between 10 and 13. I guess the input would be only numbers, so I can make use of .isaplha ? Any suggestions of how I can do it? Link to code: https://code.sololearn.com/cw25qpMZtxe2/?ref=app

27th Aug 2019, 11:12 PM
Mattie
Mattie - avatar
8 Answers
+ 2
# Lines 10-18 while True: ageRange = int(input("Enter your age: ")) if 14>ageRange>9: print("Success!") break print("Sorry, your age must be between 10 and 13\n") # Lines 112-122 if correct == answer_user: print("You are right!") else: print("Incorrect! I am sorry, " + username + " ,but that is not the right answer.") if score == nq: break If something doesn't work as expected or you don't like it please say exactly what you want and (if possible) give an example.
28th Aug 2019, 2:54 AM
Diego
Diego - avatar
28th Aug 2019, 3:53 AM
Mikhail Gorchanyuk
Mikhail Gorchanyuk - avatar
+ 1
I think the best way to get what you're looking for would be to use a function with if/else statements, rather than a loop, to get the range of ages you're looking for. Use something along these lines in the spot where you currently have your while loop: def ageRange(): age = int(input('Enter your age : ')) if age >= 10 and age <=13: print('Success!') yourNextFunction() else: print('Sorry, your age must be between 10 and 13.') ageRange() ageRange() This will call the ageRange function after it has been defined and keep looping back to it until an age between 10 and 13 is entered. Once an acceptable age is entered it will call your next function which would be whatever you decide to define that as. Hope this helps.
27th Aug 2019, 11:42 PM
Ethan Thoms
0
Hello and thank you both for your answers. What Thomas suggested might be enough for me. Cbr, I had before sys.exit() and I thought it's better to have a loop maybe. Don't know what I am missing as line 111 seems to be fine to me?...
28th Aug 2019, 12:10 AM
Mattie
Mattie - avatar
0
Cbr since the ageRange function is being called under the else statement, which will run if the age is not between 10 and 13, it will essentially call itself and ask for the age again if the age is not in range. It's not really a loop but the function calls itself if the condition is not met so in a way it's a conditional loop.
28th Aug 2019, 12:20 AM
Ethan Thoms
0
Do you have your updated code? The link is still taking me to the original code.
28th Aug 2019, 12:33 AM
Ethan Thoms
0
The code is updated and keeps sending me to line 107..
28th Aug 2019, 12:44 AM
Mattie
Mattie - avatar