Python Forum
while with a conditional test
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while with a conditional test
#1
Hallo,

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
Reply


Messages In This Thread
while with a conditional test - by driep - Apr-16-2018, 08:30 AM
RE: while with a conditional test - by j.crater - Apr-16-2018, 08:37 AM
RE: while with a conditional test - by stranac - Apr-16-2018, 08:40 AM
RE: while with a conditional test - by driep - Apr-16-2018, 12:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,142 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,130 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  How to write test cases by Unit test for database configuration file? binhduonggttn 0 2,566 Feb-18-2020, 08:03 AM
Last Post: binhduonggttn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020