Getting error message when trying to break out of a while loop in Python
<p>I'm trying to write code that includes the following:</p>

<p>1) Uses a conditional test in the while statement to stop the loop.</p>

<p>2) Uses an active variable to control how long the loop runs.</p>

<p>3) Use a break statement to exit the loop when the user enters a 'quit' value.</p>

<p><strong>Here is my code:</strong></p>

<pre><code>prompt = "What is your age?"
prompt += "\nEnter 'quit' to exit: "

while True:
    age = input(prompt)
    age = int(age)

    if age == 'quit':
        break
    elif age &lt; 3:
        print("Your ticket is free.")
    elif 3 &lt;= age &lt;=12:
        print("Your ticket is $10.")
    elif 12 &lt; age:
        print("Your ticket is $15.")
    else:
        print("Please enter a valid age.")
</code></pre>

<p>I believe I've answered part 1 and 2 correctly but whenever I enter 'quit' or any other word to test for part 3, I get an error message that states: "ValueError: invalid literal for int() with base 10: 'quit'"</p>

<p>Does anyone have any suggestions of what I may be doing wrong in my code? Thank you for your time.</p>

<p>As proposed in a comment above you should use raw_input() instead of input in order to handle the user input as a string so that you can check for the 'quit' string. If the user input is not equal to 'quit' then you can try to manage the input string as integer numbers. In case the user passes an invalid string (e.g. something like 'hgkjhfdjghd') you can handle it as an exception.</p>

<p>Find below a piece of code that demonstrates what I described above:</p>

<pre><code>prompt = "What is your age?"
prompt += "\nEnter 'quit' to exit: "

while True:
    age = raw_input(prompt)

    if age == 'quit':
        break

    try:
        age = int(age)  

        if age &lt; 3:
            print("Your ticket is free.")
        elif 3 &lt;= age &lt;=12:
            print("Your ticket is $10.")
        elif 12 &lt; age:
            print("Your ticket is $15.")

    except Exception as e:
        print 'ERROR:', e
        print("Please enter a valid age.") 
</code></pre>

python