0

I am doing an exercise from Allen Downey's book (Think Python, page 84) that says "Write a function called has_no_e that returns True if the given word doesn’t have the letter “e” in it." The author says the pattern that those exercises have in common is:

def has_no_e(word):
    for letter in word:
        if letter == 'e':
            return False
    return True

So I believe that is the answer. But how do you make this function call the words.txt list of words? I tried to do it this way but something is definitely wrong because my code just does not do anything. I see a blank line when I run it. And what I want is to make that function draw words from the words.txt file and tell me if each of them is True or False depending on the presence or absense of 'e'.

def has_no_e(list):
    fin = open(list)
    word = fin.readline()
    for letter in word:
        if letter == 'e':
            return False
    return True
has_no_e("words.txt")

Thank you.

  • You just want one True or False result indicating whether there are or aren't any e's anywhere in the words.txt file? Or do you, for example, want a list of True and False values indicating if there's an e on each line of the file? – The Photon Apr 18 '23 at 18:04
  • Also, you should say exactly what result did you expect, and what result did you get from your code. If it isn't obvious, explain how they're different. "something is definitely wrong" is not enough for us to understand your problem. – The Photon Apr 18 '23 at 18:06
  • @ThePhoton, I want a list of True and False values. The thing is I don't know how to link this function with the words.txt stored on my laptop. Well, the wrong part is that my code just does not do anything. I see a blank line when I run it. And what I want is to make that function draw words from the file and tell me if each of them is True or False depending on the presence or absense of 'e'. – romanbouchouiev Apr 18 '23 at 18:07
  • Just replace "words.txt" with a complete path to the file, like "C:\\Some directory\\words.txt". – The Photon Apr 18 '23 at 18:09
  • Please add this information to your question post, so that all the information needed to give an answer is in the post itself. – The Photon Apr 18 '23 at 18:10
  • Change the argument `list` to another variable name because list is a keyword in Python. You are using `readline` which returns the whole line instead of each individual word. You need to get the line and use `.split` to get the individual words of each line in an array of words. Then you can iterate the list of words and then each word to check if they have e. – Alias Cartellano Apr 18 '23 at 18:21
  • @AliasCartellano, thank you, I will replace the keyword list. As for the line, the txt file is basically a large list of words each written on a separate line. – romanbouchouiev Apr 18 '23 at 18:23
  • Another issue is that the function would return if it finds any words with e in them, basically meaning it wouldn't check any other word after. – Alias Cartellano Apr 18 '23 at 18:24

1 Answers1

0
 def has_no_e(list):
    fin = open(list)
    word = fin.readline()
    for letter in word:
        if letter == 'e':
            return False
    return True

This reads one line from the file, and returns True or False depending whether there is an 'e' in that line. If you want a list of results line by line, instead of returning immediately after reading the first line, you need to keep reading more lines, and accumlating the results into a data structure of some kind (for example, a list).

The more immediate problem you mentioned was

my code just does not do anything. I see a blank line when I run it.

You haven't included any code to display the result of calling has_no_e. For example, you could assign the result of has_no_e to a variable, and then use a print statement to print that variable to the console. Then you'd see what was the result of the function call.

The Photon
  • 1,242
  • 1
  • 9
  • 12