Member Avatar for Lomholdt

Hey, so i want to make a funktion that prints all the words from a list (words.txt) that doesn't contain the letter e.

I tried something like this:

def has_no_e2(word):
    fin = open('words.txt')
    for line in fin:
        word = line.strip()
        if letter in word:
            letter == ('e')
            print(word)

But i just can't make it work!
Please help.

Are you sure that the function should print words, not return them. It seems strange that you overwrite the value of parameter at line 4 without using the original value. Also line 6 does nothing so you can remove that. words.txt does not look like list for me but more like file. Variable letter is also uninitialized at line 5.

Here something you can look at.

>>> s = 'The quick brown fox jumps over the lazy dog'
>>> [i for i in s.split() if 'e' not in i]
['quick', 'brown', 'fox', 'jumps', 'lazy', 'dog']

Can break it up,code over is list comprehensions wish is much used in python.

>>> s = 'The quick brown fox jumps over the lazy dog'
>>> for item in s.split():
...     if 'e' not in item:
...         print item
...         
quick
brown
fox
jumps
lazy
dog
commented: Gave me a quick hint, and made me solve it in seconds! Excellent! +0
Member Avatar for Lomholdt

THX snippsat, I just got it working

def has_no_e2(word):
    line = fin.readline()
    for line in fin:
        word = line.strip()
        if 'e' not in line:
            print(word)

Its anoying, because i thought i already did this, but it didn't work at the time.
But at least now it does! :) Help much appreciated, some times i just need a push in the right direction!

fin is undefined, but you are discarding first line at line 2 if it is previously set to open file. Original line 2 was more correct.

Still the word paramater does not do anything and line 4 is not needed if 6 would do print(line,) or print(line, end='') depending on your Python version.

After your description this function dos what you want.
It`s more common to return out of function,than use print.
As you se it has no argument,because words comes from file.

def no_e():
    with open('fox.txt') as f:
        for item in f:
            words = item.split()
    return [word for word in words if 'e' not in word]

print no_e()

If we spilt it up it can look like this.
Now functions only dos 1 jobb an return out,this also show use of docstring.
This can be a good practise when code grow larger.

def read_file():
    '''Read a file and return words'''
    with open('fox.txt') as f:
        for item in f:
            words = item.split()
    return words

def no_e(words):
    '''Return all words,but not words that has "e" in it'''
    return [word for word in words if 'e' not in word]

if __name__ == '__main__':
    words = read_file()
    print no_e(words)
    help(read_file)

so you know, the function you created doesn't need the parameter word, but if you were abstracting the function, which is often considered better; you could make it something sort of like:

def no_e(word):
   noe=word.replace('e','')
   return noe

and then iterate through the words in the file and no_e them.

my interactive experiment with this:

>>> withh='electric slide'
>>> without=[]
>>> for word in withh.split():
	without.append(no_e(word))
>>> for word in without:
	print(word)

lctric
slid

You miss the point pyguy62 .
It shall not replace "e" in words,but print all word that not contain 'e'.

prints all the words from a list (words.txt) that doesn't contain the letter e.

You miss the point pyguy62 .
It shall not replace "e" in words,but print all word that not contain 'e'.

Thank you snippsat, I don't know how I misread that; I will add that to my top ten list of reasons not to post after 1 am, sorry guys.

Hold for that "silly" mistake, my point to op was, and remains, that he could make a function abstract and use a parameter then, as opposed to assigning a specific file in the function; it seemed to me he was a little confused about how that worked from his initial post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.