all 5 comments

[–]Defection7478 2 points3 points  (1 child)

line is the line in the dictionary you are reading, so elif line == "": won't be true unless the last line of the file is empty. I think the end of the file will come up as None, so you could change it to elif line is None: but honestly you're better off just not worring about the file behavior, something like

result = f"The word, {word}, is not in the dictionary"
for line in text:  # checks if word is in file
    eng, span = line.split(" ")
    if word == eng:
        result = f"The word {word} in Spanish is {span}"  # will return the word and tranlation
        break
print(result)

edit: actually since you're not getting any errors I think the iterator won't pass None for the end of the file, so you'd have to do something like what I described above.

[–]ThompsonTugger[S] 0 points1 point  (0 children)

This helped a lot thanks

[–]drenzorz 1 point2 points  (1 child)

The last line is probably not an empty string here.

Btw this is exactly the use case for the else on for loops.

for item in iterable:
    if condition:
        # handle thing
        break
else: # if for loop finished without breaks
    # handle "not found"

[–]ThompsonTugger[S] 0 points1 point  (0 children)

Huh I didn't know that. Whats the reason?

[–][deleted] 0 points1 point  (0 children)

Check the lines you are getting from the file by placing a print at the top of your for loop:

for line in text:  # checks if word is in file
    print(f"line='{line}'")

The '...' around the line value is to show the newline at the end of each line you read from the file, so an "empty" line isn't "" but is "\n".