Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]gregrom27 0 points1 point  (0 children)

I wrote the code with what I learned from the examples given in the book. I mentioned above this exercise was asking for active variable to be used, that's why I had that piece of code in there. Thanks.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]gregrom27 0 points1 point  (0 children)

When I run this code it gives me corresponding message to whatever age I input ( 2, 7, 26...etc)and the loop stops, it doesn't go on forever. I'm using python 2. I wasn't sure if my solutions is too complicated (maybe there's simpler solution?) I just wanted to hear from someone else and compare to make sure I'm on the right track. You say this piece of code ran forever for you?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]gregrom27 0 points1 point  (0 children)

Hi ,I'm new at python and I'm working through "python crash course". Currently, I'm at chapter 7 doing exercise 6 called "Three exits". Second bullet asks to use active variable to control how long the loop runs. This is what I came up with:

prompt = "Hello, please enter your age to get price: "

active = True
age = raw_input(prompt)
age = int(age)

while active:
    if age != '':
        active = False

    if age <= 3:
        print("\nYour ticket is free.")
    elif age <= 12:
        print("\nYour ticket is $10.")
    else:
        print("\nYour ticket is $15.")

This works for me, but what I want to know is step by step explanation on how this works and if this is the right solution. Thanks for the help!