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.
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.
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)
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.
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
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 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.
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.
Guys!
Thank you for your help and coaching!
I really appreciate it!
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