Looping through user input

I’m doing an exercise from python crash course and this one is making my head hurt. I’ve tried a dozen different ways to do this but, I am not getting it.

When I enter an age, it infinite loops thru the correct print statement. I’m trying to get it to ask for the next age input until user puts 00 to quit.

Hi @wickdw8yz

Where exactly within your while loop are you asking for this input? :wink:

You have all the necessary components to achieve what you’re looking to do, you’ve just not quite assembled them in the correct order.

2 Likes

Dang it. All the examples that I was using was showing a prompt and identifier outside of the equation. I left everything in the while statement where it was and moved my user input into the equation. Works perfect now.
Thank you @thepitycoder .

I wonder if this was because they were using the input to influence the while loop directly, like so:

# refactor

age = int(input("Enter age to find out the cost of the movie ticket, or '0' to exit: "))

while age > 0:
    if age < 3:
        print("Your ticket is free.")
    elif age < 12:
        print("Your ticket is $10.")
    else:
        print("Your ticket in $15.")

    age = int(input("Enter age to find out the cost of the movie ticket, or '0' to exit: "))
else:
    print("Thanks for using our price checker!")

This generates the following:

Enter age to find out the cost of the movie ticket, or '0' to exit: 2
Your ticket is free.
Enter age to find out the cost of the movie ticket, or '0' to exit: 5
Your ticket is $10.
Enter age to find out the cost of the movie ticket, or '0' to exit: 17
Your ticket in $15.
Enter age to find out the cost of the movie ticket, or '0' to exit: 42
Your ticket in $15.
Enter age to find out the cost of the movie ticket, or '0' to exit: 99
Your ticket in $15.
Enter age to find out the cost of the movie ticket, or '0' to exit: 123456789
Your ticket in $15.
Enter age to find out the cost of the movie ticket, or '0' to exit: 0
Thanks for using our price checker!

:slight_smile:

Yes, I think that was the idea. But it wasn’t instructing me to make it into one line, like yours. But, this book is nearly 5 years old. You have also showed me that I can make my input be an integer without making another statement for it. PCC was having me create a statement, then identify that my input was an integer.

1 Like

Would that make it the first edition of “Python Crash Course” by Eric Matthes (No Starch Press) ? If so, good choice. :+1:

To begin with, there’s certainly no harm in having distinct operations on separate lines. It makes it easier to see what’s happening and where, which can be advantageous when starting out for debugging. As you get more comfortable with Python, you’ll feel more able to nest things like that.

:+1:

1 Like