1

In Python 3, we are currently learning how to use the 'while' function. The following problem was posed to us in the chapter about 'while' functions, so I would assume I need to use the 'while' function for the problem. However, I feel like I could solve the problem using 'if' statements rather than a 'while' statement. Can someone please tell me if I'm wrong?

"A movie theater charges different ticket prices depending on a person's age. If a person is under the age of 3, the ticket is free; if they are between 3 and 12, the ticket is $10; and if they are over age 12, the ticket is $15. Write a loop in which you ask users their age, and then tell them the cost of their movie ticket."

My code:

age = input("How old are you? ")
age = int(age)
if age < 3:
     print("Your ticket is free.")
elif age > 3 and age < 12:
     print("Your ticket is $10")
elif age > 12:
     print("Your ticket is $15")

Will this solve the problem? Thanks!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Maverick
  • 49
  • 1
  • 6
  • 4
    Your code asks only once. Note that the task is to ask users, plural. – MisterMiyagi Nov 29 '18 at 20:02
  • Use a while loop to either ask multiple users for their ages, or to ask them again if they enter an invalid input. – mypetlion Nov 29 '18 at 20:03
  • It literally says *"write a loop"*, that's why. – jonrsharpe Nov 29 '18 at 20:07
  • 1
    BTW, what if someone is exactly 3 or exactly 12? You don't account for that here. – mauve Nov 29 '18 at 20:12
  • Read [Ask user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) - this is another application of the `while` to avoid joke-inputs like `I am fourty-nine` which currently will crash your program because the `int()` conversion can not handle the input and throws an exception. Read about exceptions here: https://docs.python.org/3/tutorial/errors.html#handling-exceptions or from the answers in the link to the question `Ask ... ` I provided. – Patrick Artner Nov 29 '18 at 20:20

4 Answers4

4

Write a loop in which you ask users their age, and then tell them the cost of their movie ticket

You have to ask multiple users their age in loop

Lev Leontev
  • 2,538
  • 2
  • 19
  • 31
1

As pointed out already, your program should loop to allow many users to get the ticket price. Always make sure that you do not get stuck in an infinite loop, however, so you need to consider a way to escape the loop. I would suggest that the user can enter something like "Q" to quit.... however then you must consider: uppercase or lowercase... making the user input lowercase (only) for comparison takes care of this, so entering a "Q" will allow the loop to exit using the break statement.

Next, you should also consider that a user may enter "ten" for example, so to stop the float() "blowing up" and spitting an exception, using a try/except would handle this.

it is essentially "try to execute this code, and if it doesn't explode, continue" and except is like the "else" when using an if statement.

I hope this explains the need for a loop, but also other considerations when writing such programs, and how you might approach handling them.

while True:
    age = input("\nHow old are you? (Q to quit)")
    if age.lower() == "q":
        break
    try:
        age = int(age)
        if age <= 3:
             print("Your ticket is free.")
        elif age > 3 and age <= 12:
             print("Your ticket is $10")
        elif age > 12:
             print("Your ticket is $15")
    except:
        print("Invalid entry")
print("\nGoodbye")
srattigan
  • 665
  • 5
  • 17
  • `age = input("\nHow old are you? (Q to quit)").lower()` works as well – Patrick Artner Nov 29 '18 at 20:26
  • and in this special instance you could get away without `try: except:` by using `if age.strip().ìsdigit():` ... but your solution is superior and more advanced. – Patrick Artner Nov 29 '18 at 20:28
  • Thanks Patrick. I figured it might be easier for a learner to grasp... – srattigan Nov 29 '18 at 20:30
  • your `elif`'s can be rewritten to `elif 3 < age < 12:` (nitpicking ;o) ) and if the ranges would be better specified so that 3y and 12y are able to purchase a ticket as well you could even go for `elif age<12:` because all youngers would have been captured by the earlier `if age < 3:` code block – Patrick Artner Nov 29 '18 at 20:30
0

As per Leo stated, this is asking multiple users. Use:

age = None
while age is not "done": #or while True: for infinitely asking
    #insert your code

This will keep asking for an age until "done" is input

Jab
  • 26,853
  • 21
  • 75
  • 114
-1

'While' statement in this exercise is not for the 'age' variable but for the 'ask' process.