Python Forum

Full Version: Practicing using a "flag": please point in right direction
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm currently going through the Python Crash Course and I'm on Chapter 7. I'm attempting to use an active variable to control how long the loop runs.

prompt = "\nWhat is your age?"
prompt += "\n(Enter 'quit' when you are finished.)" 

active = True 
while active: 
    age = input(prompt) 
    
    if age == 'quit': 
        active = False 
    age = int(age)
    if age < 3:
        print("\nYour ticket is free!") 
    elif age < 12: 
        print("\nYour ticket is 10$!") 
    else:
        print("\nYour ticket is 15$!") 
        
The above works until I enter quit.
Error:
Traceback (most recent call last): File "parrot.py", line 10, in <module> age = int(age) ValueError: invalid literal for int() with base 10: 'quit'
I know that this is because I change age data type to an integer data type so I can use the comparison operators However, quit is not an int. I feel like maybe this is a matter of placement? I've tried different placements (before the if age == 'quit') but it's still not working. Could someone please point me in the right direction? Thank you!
The problem is that after running line 9, the code goes on with line 10. Why don't you simply replace line 9 with the break statement?
it's more common to skip the flag

while True: 
    age = input("\nWhat is your age?\n(Enter 'quit' when you are finished.)" ) 
    if age == 'quit': 
        break
    age = int(age)
    if age < 3:
        print("\nYour ticket is free!") 
    elif age < 12: 
        print("\nYour ticket is 10$!") 
    else:
        print("\nYour ticket is 15$!") 
(May-09-2019, 08:03 PM)Gribouillis Wrote: [ -> ]The problem is that after running line 9, the code goes on with line 10. Why don't you simply replace line 9 with the break statement?
Hi! So I actually have that already done but I'm trying to practice using a flag (following the practice problems in the book) so I'm attempting to revise my code so it adds a flag.

Here is my code with a break:

prompt = "\nWhat is your age?"
prompt += "\n(Enter 'quit' when you are finished.)" 

while True:
    age = input(prompt) 
    
    if age == 'quit': 
        break
    age = int(age)
    if age < 3:
        print("\nYour ticket is free!") 
    elif age < 12: 
        print("\nYour ticket is 10$!") 
    else:
        print("\nYour ticket is 15$!") 
        

(May-09-2019, 08:10 PM)buran Wrote: [ -> ]it's more common to skip the flag
while True: age = input("\nWhat is your age?\n(Enter 'quit' when you are finished.)" ) if age == 'quit': break age = int(age) if age < 3: print("\nYour ticket is free!") elif age < 12: print("\nYour ticket is 10$!") else: print("\nYour ticket is 15$!") 
Hi sorry! First, ty for your reply! I just posted below but I do already have a version of this with break. I'm just trying to practicing using a flag now. In addition, what do you mean by it's more common to skip a flag? Flags are not commonly used when coding? Is that a correct understanding of what you said? Thank you so much again!
For a case without using a break one would generally use only one main if block (to prevent potential unintended fall-trough into the other if's).

In your initial case that could have been done by using the else option of the if age == 'quit': condition.
ie:
#...
active = True 
while active: 
    age = input(prompt)

    if age == 'quit':
        active = False

    else: ## do stuff if age was not equal to 'quit'.
        age = int(age)

        if age < 3:
            print("\nYour ticket is free!")
        elif age < 12:
            print("\nYour ticket is 10$!") 
        else:
            print("\nYour ticket is 15$!")
(May-09-2019, 08:13 PM)magsloo Wrote: [ -> ]I'm trying to practice using a flag (following the practice problems in the book) so I'm attempting to revise my code so it adds a flag.

I personally would recommend not to practice using flags. In majority of cases there is 'pythonic' way to have concise and more readable code without flag.