Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confused with 'flags'
#1
Greetings!
I'm confused about how to use 'flag', I read I can use it instead of 'ELSE'.
I'm playing with the code below to see how I can use "flags" but it does not work.
with open ('C:/02/somefile.txt','r') as f :
    flg = False
    for l in f :
        if '123' in l : 
            print (l)        
            flg = True 

    if flg == False :
        print (" 'ELSE' -->> "+l)
Thank you.
Reply
#2
I think your indentation is wrong, or I am not following your logic. Lines 8 and 9 should be indented one more.

Also, picky thing, rather than if flg == False, recommend just saying if !flg
With booleans you can simply test the variable.
Reply
#3
I do not understand what the 'flag' does or what I can go without it.
It makes no sense (for me) how and when to use it.
I read I can use it instead of "ELSE".
The code below is to see what the 'flag' does,
I'm moving the 'flg' back and forth to see what it would do.
I tried your suggestion and replaced
    if flg == False:
With
    if flg :
And I can print "ELSE" the last line from my "somefile.txt' for some reason, not the first line that not matching '123', which added even more confusion because both statements are equal.
Thank you for the quick reply!


with open ('C:/02/somefile.txt','r') as f :
    flg = False
    for l in f :
        if '123' in l : 
            print (l)        
            flg = True 

    if flg :
        print (" 'ELSE' -->> "+l)
Reply
#4
Not sure if I follow but here is an example

flg = False
with open('some.txt', 'r') as f:
    for line in f:
        if '123' in line: # This will print if 123 is True
            print(line)
            flg = True
        if '123' not in line:
            print(f'Else -->> {line}') # Flag is still False
Output:
This line has 123. Else -->> This line does not. Else -->> This line does not. This line does have 123. Else -->> This line does not.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#5
if '123' is not in line :
is the same as having
else :
You can remove 'flg = False' and 'flg = True' completely and the code will run just fine...

with open('some.txt', 'r') as f:
    for line in f:
        if '123' in line: # This will print if 123 is True
            print(line)
        if '123' not in line:
            print(f'Else -->> {line}') # Flag is still False
question,
Can I replace 'else' or "if not in' with the 'flag'?
Thank you
Reply
#6
Again not sure what you're after.
# text file without 123 in a line
This line has not.
This line does not.
This line does not.
This line does not.
This line does not.
# text file with 123 in a line
This line has not.
This line does not.
This line does have 123 in it.
This line does not.
This line does not.
flg = False
with open('some.txt', 'r') as f:
    for line in f:
        if '123' in line: # This will print if 123 is True and set flag to True
            print(line)
            flg = True
    if flg: # Check if flg is True or False
        print('Flag is set to true')
    else: # flg is still set to False
        print('Flag is set to false')
First output
Output:
Flag is set to false
Second output
Output:
This line does have 123 in it. Flag is set to true
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
I do not get what the variable "flag",
Why I need them if I can go everything with "if', "elif" and "else".
I read somewhere "flag" can replace "else", I tried to see how that is possible.
I see it is not. I'm sure 'flag' has some use, I just did not discover its usage yet.
Reply
#8
One uses a flag to enable or disable something. It still needs to be tested with an if so that you know whether to do whatever it's controlling. Using an else isn't mandatory - sometimes, you don't want to do anything in the case the flag is off (i.e. set to False).

When developing real world software, "feature flags" are a well-used technique (especially if you're doing trunk-based development). They allow you to control when a new feature is enabled - while developing the feature, the flag can be turned off so that the code is integrated but not exposed and when you think it's done, you may want to enable it only on a testing environment first before enabling it in production. The latter is beneficial because even though one writes unit and integration tests, those tests are quite limited of course and the real world data or environment can throw up some unexpected issues. One usually sets the flags in the application's configuration.
tester_V likes this post
Reply
#9
Guys!
Thank you for your help and coaching!
I really appreciate it!
Reply
#10
The term "flags" is used to mean a lot of different things, but I think you are talking about using a variable to signal that something happened. In this context flags are just variables. You can use a flag in an if-else statement, but a flag is not a substitute for else.
found = False  # I have not found a line containing 123
with open('C:/02/somefile.txt','r') as file:
    for line in file:
        if '123' in line: 
            found = True  # I found a line
            break

if found:  # Did I find a line?
    print(line)
    # Do lots of additional processing
A flag is used when there is space between finding and acting on the condition you are interested in. In the example below verbose is a flag used to signal that '--verbose' was contained in the command line arguments.
import sys

verbose = '--verbose' in sys.argv:

with open(sys.args[1], 'r') as file:
    for line in file:
        if verbose:
            print(line)
        if '123' in line: 
            break
tester_V likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,016 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  I am confused with the key and value thing james1019 3 912 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,464 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,625 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Regex - Pass Flags as a function argument? muzikman 6 3,482 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Passing flags to python script, through a function xbit 4 3,874 Apr-20-2021, 06:32 AM
Last Post: ndc85430
  Simple Tic Tac Toe but I'm confused Izith 1 2,152 Sep-26-2020, 04:42 PM
Last Post: Larz60+
  I am really confused with this error. Runar 3 2,925 Sep-14-2020, 09:27 AM
Last Post: buran
  Confused on how to go about writing this or doing this... pythonforumuser 3 2,420 Feb-10-2020, 09:15 AM
Last Post: snippsat
  Dazed and confused... RodNintendeaux 10 7,388 May-28-2017, 01:32 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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