Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my for loop run indefinitely and doesn't stop when the if condition is met?

I’m trying to read text from a file and using a loop to find a specific text from the file. The data in the file is listed vertically word by word.
When I run the script, after it prints the last word in the file it repeats itself from the beginning indefinitely.

with open('dictionary.txt','r') as file:
    dictionary = file.read().strip()
for i in dictionary:
 print (dictionary)
 if( i == "ffff" ):
    break

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

first split the lines by "\n" then print(i) not print(dictionary):

with open('dictionary.txt', 'r') as file:
    dictionary = file.read().strip().split("\n")
for i in dictionary:
    print(i)
    if i == "ffff":
        break
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading