Python Forum
if conditions in the for indentation doesnt work ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if conditions in the for indentation doesnt work ?
#1
Hello Everyone,

I am trying to get the lines that contain test string or if its not found i want to print not found message. However this doesnt seem to work i dont get any messages at all.

I can read text file, i can even make it count with "for line in input:" however none of the if conditions work inside for indentation. Am i missing something ?



def verial():
  
    isimsatırlistesi=list()
    satırsayısı=0
    input = text1.get("1.0",'end-1c')
    for line in input:
        satırsayısı=satırsayısı+1
        if "test" in line:
            isimsatırlistesi.append(int(satırsayısı))
            print(isimsatırlistesi[0,"end"])
            if len.isimsatırlistesi<0:
                print("test not found")
Reply
#2
Because everything after line 6 is indented, they are all checked for every element in "input". Did you mean to run them every time, or perhaps some of them are only to be run after the input in exhausted?

Because everything after line 8 is indented further, lines 9-12 are only executed when "test" is found in line. If that test fails, nothing else will be executed. If you want something to happen when the test fails, you'd need an else condition, or just do it after the conditional is done (would happen regardless of the if statement).

if "test" in line:
    # this is only run when the if expression is true
    isimsatırlistesi.append(int(satırsayısı))
    print(isimsatırlistesi[0,"end"])
else:
    # this section is only run when the if expression is false
    print("Condition failed.")

# This section is run after, regardless of the if expression.
    
Reply
#3
Thanks for the answer, interestingly this code cant seem to find test, i tried if line.startswith method to no success.
Reply
#4
Probably I miss something but what these lines are supposed to do:

print(isimsatırlistesi[0,"end"])

if len.isimsatırlistesi<0:
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
#5
(May-04-2020, 10:11 AM)perfringo Wrote: Probably I miss something but what these lines are supposed to do:

print(isimsatırlistesi[0,"end"])

if len.isimsatırlistesi<0:

print(isimsatırlistesi[0,"end"]) this prints the list of isism satır listesi

if len.isimsatırlistesi<0: and this checks if number of elements in isim satır listesi is less than 0 it returns no string found message.
Reply
#6
(May-03-2020, 10:08 PM)Sutsro Wrote: Hello Everyone,

I am trying to get the lines that contain test string or if its not found i want to print not found message. However this doesnt seem to work i dont get any messages at all.

I can read text file, i can even make it count with "for line in input:" however none of the if conditions work inside for indentation. Am i missing something ?



def verial():
  
    isimsatırlistesi=list()
    satırsayısı=0
    input = text1.get("1.0",'end-1c')
    for line in input:
        satırsayısı=satırsayısı+1
        if "test" in line:
            isimsatırlistesi.append(int(satırsayısı))
            print(isimsatırlistesi[0,"end"])
            if len.isimsatırlistesi<0:
                print("test not found")

I have already forked Python to have proper { } blocks and get rid of this fancy indentation rules. If there is anybody interested I can make my git become public or share the diff file. To be clear, the forked version can still continue to work on indented-style files, you can just decide every time you open a statement if you want to use the indent-style or { }.
Reply
#7
block1 = """This is a bit
of text but
should pass completely
"""

block2 = """This bit of
text will be found
to be a real test of your patience
"""

for text_input in (block1, block2):
    line_count = 0
    for line_count, line in enumerate(text_input.splitlines(), start=1):
        if "test" in line:
            print(f"There were {line_count-1} lines before finding 'test'")
            break
    else:
        print(f"test not found")
Output:
test not found There were 2 lines before finding 'test'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesnt chunk generation work? LotosProgramer 0 76 Jun-18-2021, 01:13 PM
Last Post: LotosProgramer
  I have two Same Code but One of them Doesnt Work beginner721 6 490 Jan-22-2021, 10:56 PM
Last Post: beginner721
  code doesnt return anything ofrihemo 3 688 Jun-30-2020, 05:14 PM
Last Post: ofrihemo
  Pydoc documentation doesnt work Cosmosso 4 1,328 Dec-13-2019, 08:21 AM
Last Post: Cosmosso
  multiprocessing doesnt contain some modules(shared_memory) mike000 3 726 Oct-31-2019, 02:20 PM
Last Post: mike000
  BEGINNER: My calculator doesnt work iskov 5 958 Oct-09-2019, 07:45 AM
Last Post: buran
  Why this simple function doesnt work? blackknite 8 1,396 Jan-05-2019, 12:32 PM
Last Post: buran
  why doesnt the while loop run? supermane 3 1,314 Aug-04-2018, 06:01 AM
Last Post: wavic
  Why does this work and this doesnt= puruvaish24 1 1,188 May-22-2018, 03:58 AM
Last Post: scidam
  Discord Bot doesnt work when opened with windows himdipie 0 1,286 Apr-15-2018, 04:48 PM
Last Post: himdipie

Forum Jump:

User Panel Messages

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