Python Forum
Practicing using a "flag": please point in right direction
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Practicing using a "flag": please point in right direction
#1
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!
Reply
#2
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?
Reply
#3
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$!") 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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!
Reply
#5
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$!")
Going beyond the idiomatic Python (Sensible Idiomatic usage - Blog post - Sep 11th 2017)
Reply
#6
(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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,166 Mar-16-2022, 08:54 PM
Last Post: cubangt
  Project Direction bclanton50 1 1,323 Jan-06-2022, 11:38 PM
Last Post: lucasbazan
Question How to understand the vector/direction mason321 0 1,105 Dec-14-2021, 10:57 PM
Last Post: mason321
  how to check for thread kill flag nanok66 1 2,168 May-09-2020, 10:06 PM
Last Post: nanok66
  Pointer in the right direction? Viking 5 2,722 Apr-22-2020, 06:14 PM
Last Post: Viking
  Check for a special characters in a column and flag it ayomayam 0 2,043 Feb-12-2020, 03:04 PM
Last Post: ayomayam
  Using a flag error blackjesus24 1 1,610 Jan-30-2020, 09:42 AM
Last Post: buran
  connecting the first point to the last point Matplotlib omar_mohsen 0 4,583 Jan-15-2020, 01:23 PM
Last Post: omar_mohsen
  Length and direction cosines of lines tarikrr 1 1,762 Nov-15-2019, 04:16 AM
Last Post: SheeppOSU
  Some direction needed Patriot1017 3 2,484 Sep-03-2019, 05:44 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020