0

First a little about the program itself:

  • A conditional test begins the while loop (I have to use a conditional test to begin and end the loop, No flags and no break statements)
  • Asks for users age (user input)
  • Depending on the user input, it prints out different answers

My problem is that I want the program to end if the user input is 'quit'. The user input will besides 'quit' always be an int, because the program checks the users age.

This is my code:

prompt = "\nPlease enter your age to see the price for a ticket. \nEnter 'quit' when done: "

age = ""

while age != "quit":
    age = input(prompt)
    age = int(age)
    if age < 3:
        print("Your ticket is free.")
    elif age > 3 and age < 12:
        print("Ticket is $10")
    else:
        print("Ticket is $15")

This is the error i get when i put 'quit' as the input:

Please enter your age to see the price for a ticket.  Enter 'quit' when done: quit 
Traceback (most recent call last):   File "while_loops.py", line 60, in <module>
    age = int(age) ValueError: invalid literal for int() with base 10: 'quit'

Many thanks in advance for your time and effort! Best regards HWG.

2
  • 1
    raw_input is py2 only. Commented Jan 13, 2017 at 2:57
  • You can chain comparisons: 3 < age < 12 Commented Jan 13, 2017 at 4:58

2 Answers 2

3

Check for quit before you try to convert to int. Run in an infinite loop and break out of it when you read the input quit.

prompt = "\nPlease enter your age to see the price for a ticket. \nEnter 'quit' when done: "

while True:
    age = input(prompt)
    if age == "quit":
        break
    age = int(age)
    if age < 3:
        print("Your ticket is free.")
    elif age > 3 and age < 12:
        print("Ticket is $10")
    else:
        print("Ticket is $15")

Alternative which meet added criteria

not use 'break' or a variable as a flag

Using an exception

while ageStr != "quit":
    ageStr = input(prompt)

    try:
        age = int(ageStr)
        if age < 3:
            print("Your ticket is free.")
        elif age > 3 and age < 12:
            print("Ticket is $10")
        else:
            print("Ticket is $15")
    except ValueError:
        pass

Using continue.
Note: this is bad as you specify and check for "quit" twice in the code and the control flow is overly complicated.

prompt = "\nPlease enter your age to see the price for a ticket. \nEnter 'quit' when done: "

age = ""

while age != "quit":
    age = input(prompt)
    if age == "quit":
        continue

    age = int(age)
    if age < 3:
        print("Your ticket is free.")
    elif age > 3 and age < 12:
        print("Ticket is $10")
    else:
        print("Ticket is $15")
Sign up to request clarification or add additional context in comments.

6 Comments

The assignment was to not use 'break' or a variable as a flag. I have to use a conditional test in the 'while' statement. I dont know if its a trick question and not possible or?
what about continue or an exception? Otherwise I dont see how it can work. I wish they would teach the language as its supposed to be used, rather than having these weird limitations that force you to write non idiomatic code.
Can i use continue to make it work? If yes, then how?
A few more options, they are not recommended for real use though. I'd be interested to find out what the actual answer your teacher had in mind was. if it was either of these latter two I'd be shocked.
Also note you have a bug. If you enter the age as 3, it will drop through to the else clause, as your first check is for less than 3 and your second check is for greater than 3. The consequence is that as 3 doesnt meet either criteria it triggers the else ($15 tickets for 3 year olds).
|
2
while True:
    age = input("\nPlease enter your age to see the price for a ticket. \n Enter 'quit' when done: '"
    if age == 'quit':
        break
    # continue with the rest of your code

Just check to see if the input is 'quit', if so, break out of the infinite loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.