Hi everybody,

Again, I try to deal with exercise from a book Think Python version 1.1.19., exercise 9.2. (case study: world play)
My contents of the words.txt is:

"Ahoj,
Toto je moj prvy pokus otvorit a citat subor.
Vela stastia "

Thing is, if you have a look at a scrip below you will see that the result is just showing whole line. for example 1 line (Ahoj,) doesn't contain e in the word Ahoj, so result is Ahoj,. But 2 line is more interesting. There are more words in the line so I am interesting to show (as a result) words which doesn't contain e. As you can see, result should be following:

Toto moj prvy pokus otvorit a citat subor.

As you can seen 'je' is omited. I think that is the case of the exercise, but I don't know how to read word by word in all lines, for that I would approciate any raesonable hint because I am stuck.

def main():
    fin = open('words.txt')

    for words in fin:
        word = words.strip()
        if has_no_e(word):
            print word

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

main()

Thank you Vlady

Recommended Answers

All 7 Replies

You are reading lines of the text file, not individual words. To get to the individual word study this hint ...

line = "Toto je moj prvy pokus otvorit a citat subor."

word_list = line.split()

for word in word_list:
    print word

Note:
Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.

thank you very much! I will try it and let you know :-)

thank you very much! I will try it and let you know :-)

I don't know, I've tried this but it's not what I've ment. I 'd like to leave words which contain the letter 'e', and don't know how. This script just omit 'e' in the text.

def test():
    fin = open('words.txt')
    for line in fin:
        word = line.split()
        for i in word:
            for k in i:
                if k !='e':
                    print k,

test()

thank you

vlady

You are going too far in your code, change it to ...

def test():
    fin = open('words.txt')
    for line in fin:
        word_list = line.split()
        for word in word_list:
            if 'e' in word:
                continue   # skip to next word
            print word,

test()

You are going too far in your code, change it to ...

def test():
    fin = open('words.txt')
    for line in fin:
        word_list = line.split()
        for word in word_list:
            if 'e' in word:
                continue   # skip to next word
            print word,

test()

YES!!! YOU ARE GREAT! Thank you very much! Thing is I didn't know the command 'continue' but you showed me :-)

Could you mark this as solved?

it was done

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.