GJ01
GJ01

Reputation: 71

The 'remove vowels' function only prints the first letter of a 'readline( )', what am I doing wrong?

Summary:

Write a program that processes the contents of “pc_woodchuck.txt,” line by line. It creates an output file in the current working directory called “pc_woodchuck.tmp,” which has the same contents as “pc_woodchuck.txt,” except that all the vowels are removed (case-insensitively). At the end, display how many characters you read, and how many characters you wrote.

Create a textfile (pc_woodchuck.txt) with lines below to get same results:

Hoeveel hout kan een houthakker hakken
Als een houthakker hout kan hakken?
Hij kan hakken zoveel als hij kan hakken
En hij hakt zoveel als een houthakker kan hakken
Als een houthakker hout kan hakken.

Attempt so far:

from os.path import join
from os import getcwd

def removeVowels( line ):
    newline = ""
    for c in line:
        if c not in "aeiouAEIOU":
            newline += c
        return newline

inputname = join( getcwd(), "pc_woodchuck.txt" )
outputname = join( getcwd(), "pc_woodchuck.tmp" ) # This will be the copy of the 
                                                  # textfile at 'inputname' without 
                                                  # vowels (pc_woodchuck.tmp).                                 

fpi = open( inputname )
fpo = open( outputname, "w" )

countread = 0
countwrite = 0

while True:
    line = fpi.readline()
    if line == "":
        break
    countread += len( line )
    line = removeVowels( line )
    countwrite += len( line )
    fpo.write( line )

fpi.close()
fpo.close()

print( "Read:", countread )
print( "Wrote:", countwrite )

Ouput so far:

Read: 201
Wrote: 2    # But there must be more than two vowels!

Question:

What am I doing wrong? The result 'Wrote: 2' is clearly not right...

Upvotes: 2

Views: 86

Answers (1)

Jiahao Cai
Jiahao Cai

Reputation: 1250

return newline is in the for-loop, so the function return in the first loop, that is why there is only one letter.

I think it should be:

def removeVowels( line ):
newline = ""
for c in line:
    if c not in "aeiouAEIOU":
        newline += c
return newline

Upvotes: 2

Related Questions