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
8 Réponses
+ 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.
+ 8
Fixed code:
https://code.sololearn.com/cn2bC14W8q09/?ref=app
+ 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.
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?...
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.
0
Do you have your updated code? The link is still taking me to the original code.
0
The code is updated and keeps sending me to line 107..