Monday, March 26, 2012

exercise 9.2 - has_no_e


"""=====================
exercise 9.2
====================="""

def has_no_e(word):
    char = ""
    for char in word:
        if (char == "E") or (char == "e"):
            return False
    return True

fin = open('words.txt')

total_words = 0
words_without_e = 0
percent_without_e = 0.0

for line in fin:
    word = line.strip()
    if has_no_e(word):
        print word
        words_without_e = words_without_e + 1
    total_words = total_words + 1

percent_without_e = ( words_without_e / total_words ) * 100

5 comments:

  1. Hi, I run the program but it didn't work.

    ReplyDelete
  2. Run it, and add print(word) before c2=c2+1 if you want the list of words without e as well.

    fin= open('words.txt')
    c1=0
    c2=0
    for line in fin:
    line=fin.readline()
    word=line.strip()
    c=0
    c1=c1+1
    for letter in word:
    if letter=='e':
    c=c+1
    if c<1:
    c2=c2+1
    print(((c1-c2)/c1)*100)

    ReplyDelete
  3. Run it, and add print(word) before c2=c2+1 if you want the list of words without e as well.

    fin= open('words.txt')
    c1=0
    c2=0
    for line in fin:
    line=fin.readline()
    word=line.strip()
    c=0
    c1=c1+1
    for letter in word:
    if letter=='e':
    c=c+1
    if c<1:
    c2=c2+1
    print(((c1-c2)/c1)*100)

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. what do you think about my solution :

    # thinkpython2 python version 3.5
    # the solution of the exercice n° 9.2

    i=0

    j=0

    fin = open('words.txt')

    for line in fin:

    ____word = line.strip()

    ____j=j+1 # j is the total number of words in th text file


    ____if 'e' not in word :

    ________print(word)

    ________i=i+1 # is the total number of the letter e in the text file

    print(i*100/j,"%")

    ReplyDelete

Note: Only a member of this blog may post a comment.