Python noobie question

Hi, I’m going through the Python crash course and I have a question regarding one of the exercises, it’s asking me to code a program for assigning ticket prices depending on the person’s age, I have no problem with the exercise itself, but I would like to add a condition for the program to stop if I enter “quit” on my input, the problem is that the input is for integers, is there a way to add both conditions? (str and int) here’s what I tried as of now, sorry if it’s a silly question, cheers thanks

message = "Welcome to the cinema, how old are you?: "
age = int
age = ""
while age != "quit":
     age = int(input(message))
     if age <= 3:
         print("Your ticket is free")
     elif age <= 12: 
         print("Your ticket will cost $10")
     else: 
         print("Your ticket will cost $15")

Before converting the value from input() to an int check if it is ‘quit’.

2 Likes

Some observations.

Why to have age = int? What should it do especially if on next row this name is bound to new object?

>>> age = int
>>> age
<class 'int'>
>>> age += 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +=: 'type' and 'int'

You can use assignment expression to make some lines redundant (requires 3.8 <= Python).

while (age := input(message).lower()) != "quit": 
    # do your stuff
1 Like

Thanksss for the reply, it might not be the cleanest code but I made it work like this:

message = "Welcome to the cinema, how old are you?: "
age_factor = input(message)
while age_factor != "quit":
    age_factor = int(age_factor)
    if age_factor <= 3:
        print("Your ticket is free")
        break
    elif age_factor <= 12:
        print("Ticket will be 10 USD")
        break
    else:
        print("Ticket will be 18 USD")
        break

Hi, I tried the line of code you wrote it also works but I’m not familiarized with the assignment expression, I’ll definitely take a deep look into it later on though, Thanks!!

I’d just like to point out that there’s no point in using a while loop if all the branches within it end with break, which will make it leave the loop.

2 Likes

Hi Matthew, I tried doing it without the break but the input answer kept repeating itself infinitely :sweat_smile:

message = "Welcome to the cinema, how old are you?: "
age_factor = input(message)
while age_factor != "quit":
     age_factor = int(age_factor)
     if age_factor <= 3:
          print("Your ticket is free")
     elif age_factor <= 12:
          print("Ticket will be 10 USD")
     elif age_factor > 12:
          print("Ticket will be 18 USD")

Thanks all, I figured it out

message = "Welcome to the cinema, how old are you?: "
age_factor = ""
while age_factor != "quit":
    age_factor = input(message)
    if age_factor != "quit":
        age_factor = int(age_factor)
        if age_factor <= 3:
          print("Your ticket is free")
        elif age_factor <= 12:
          print("Ticket will be 10 USD")
        elif age_factor > 12:
          print("Ticket will be 18 USD")

You do not need the if test. After you convert age_factor to an int that test is always True.

In your code with three break statements the while statement was working as a different (non-loop) statement. You did not give the loop chance to loop :slight_smile: All the conditions ended in break.

See the solution if the hint is not enough: So what about replacing while by if?