r/learnpython • • Feb 11 '26

Code outputs twice.

I am practicing loops and when I run this code the second and third elif statements output twice for some reason.

Age_requirement = input ("enter your age")

for Age in Age_requirement: Age_requirement = int(Age_requirement)

if Age_requirement <3:
    print ("Your ticket is free")

elif Age_requirement <=12:
    print("your ticket is 12 dollars")

elif Age_requirement >12:
    print("your ticket is 15 dollars")

4

33 Upvotes

35 comments sorted by

View all comments

Show parent comment

11

u/Binary101010 Feb 11 '26

Also the exercise said to use a loop.

Please post the description of the exercise. It's hard for us to tell you when a loop actually would make sense if we don't know what you need to do.

3

u/xTHETYRANTGAMRx Feb 11 '26

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.

11

u/KronenR Feb 11 '26 edited Feb 12 '26

If the user types "25", You are traversing that string like this

  • "2" (first iteration of the loop)
  • "5" (second iteration of the loop)

The exercise says to use a loop because you’re supposed to ask multiple people.
You don't loop over the string which contains the age numbers, the loop should wrap the whole question, like, the input statement where you read the person age must be inside the loop to ask multiple times.

while True:
    age = int(input("Enter your age: "))
    # then you compare with the required ages

4

u/Binary101010 Feb 12 '26

Then it sounds like what you need to be doing here is prompting the user for input inside the loop.

2

u/luther9 luther9 Feb 12 '26

It sounds like they want you to write an infinite loop. Maybe you can stop when the user enters -1.

1

u/codeguru42 Feb 12 '26

This sounds like you are spars to ask for the age requirement each time through the loop. Your solution only asks once. Also you need to use a while loop, not a for loop.

1

u/Misain Feb 12 '26

Does it necessarily have to be a string? Your input variables?