Apr-16-2018, 08:30 AM
Hallo,
I am new to python and am busy studying the while loop.
I have the following bit of code:
But when entering 'quit' as the input, the while loop does not terminate as expected but execute the first if statement and then, as expected, produce an error:
"Age: quit
Traceback (most recent call last):
File "<ipython-input-30-17864134defc>", line 8, in <module>
if int(age) < 3:
ValueError: invalid literal for int() with base 10: 'quit'"
Can anyone indicate the (probably) obvious reason why the while loop does not terminate
I am new to python and am busy studying the while loop.
I have the following bit of code:
age = "" prompt = "\nPlease enter your age: " prompt += "\n(Enter 'quit' to end the program.) " while age.lower() != 'quit': # this is to make sure that if the user enters Quit quiT etc. it will be correctly processed age = input(prompt) print("Age: " + age) if int(age) < 3: print("\tYour ticket is FREE !") elif 3 <= int(age) <= 12: print("\t-->Age = " + age) print("\tYour ticket will be $10 !") else: print("\t-->Age = " + age) print("\tYour ticket will be $15 !")Running the code and entering a valid integer value, the code executes fine
But when entering 'quit' as the input, the while loop does not terminate as expected but execute the first if statement and then, as expected, produce an error:
"Age: quit
Traceback (most recent call last):
File "<ipython-input-30-17864134defc>", line 8, in <module>
if int(age) < 3:
ValueError: invalid literal for int() with base 10: 'quit'"
Can anyone indicate the (probably) obvious reason why the while loop does not terminate