2 Replies - 10575 Views - Last Post: 07 June 2011 - 12:11 AM Rate Topic: -----

#1 itdoell   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 239
  • Joined: 13-January 11

Python script only reading first word of each line.

Posted 06 June 2011 - 05:24 PM

A friend of mine is learning Python (something I know little to nothing about) and he's run into an issue in which his script is only printing the first word in each line, rather than every word. Could someone please help us figure this out?

def piglatin(line):
for word in line.split():
vowels='aeiouAEOIU'
if word == 'T':
return 'Iway'
break
elif word[0] in vowels:
return word + ' ' + '-->' + ' ' + word + 'hay' + '\n'
elif word[1] in 'aeiouAEOIU':
return word + ' ' + '-->' + ' ' + word[1:] + word[0] + 'ay' + '\n'
elif word[2] in vowels:
return word + ' ' + '-->' + ' ' + word[2:] + word[0] + word[1] + "ay" + '\n'
elif word[3] in vowels:
return word + ' ' + '-->' + ' ' + word[3:] + word[0] + word[1] + word[2] + "ay"
+ '\n'
elif word[0]=='q' and word[1]=='u':
return word + ' ' + '-->' + ' ' + word[2:] + word[0] + word[1] + "ay" + '\n'
elif word ==' ':
return ' '
elif word[0]=='ABCDEFGHIJKLMNOPQRSTUVWXYZ':
return word + ' ' + '-->' + ' ' + word[1:] + word[1].upper() + word[0].lower() + 'ay'
+ '\n'

x = raw_input('Please give a name of a file: ')
def main(x):
inputfile = open(x,'r')
outputfile = open(x + 'piglat.txt','w')
line = inputfile.readline()
while line!='':
print piglatin(line)
outputfile.write(piglatin(line))
line=inputfile.readline()

inputfile.close()
outputfile.close()

main(x)


So he needs this script to read every word, perform the modifications (which work) and print each word separately. Any help is appreciated, thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Python script only reading first word of each line.

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 828
  • View blog
  • Posts: 2,236
  • Joined: 23-December 08

Re: Python script only reading first word of each line.

Posted 06 June 2011 - 08:19 PM

One important feature of Python is that indentation matters. In some languages scope is determined by curly brackets, in Python it's all about the indentation. Everytime you see a colon (scoping operator), the next line needs to be indented. I'm sure that the code originally was indented correctly and it got messed up when you copy-pasted it.

Here's the code cleaned up a bit
def piglatin(line):
    vowels='aeiouAEOIU'
    for word in line.split():
        
        if word == 'T':
            return 'Iway'

        elif word[0] in vowels:
            return word + ' ' + '-->' + ' ' + word + 'hay' + '\n'

        elif word[1] in 'aeiouAEOIU':
            return word + ' ' + '-->' + ' ' + word[1:] + word[0] + 'ay' + '\n'

        elif word[2] in vowels:
            return word + ' ' + '-->' + ' ' + word[2:] + word[0] + word[1] + "ay" + '\n'
        elif word[3] in vowels:
            return word + ' ' + '-->' + ' ' + word[3:] + word[0] + word[1] + word[2] + "ay" + '\n'

        elif word[0]=='q' and word[1]=='u':
            return word + ' ' + '-->' + ' ' + word[2:] + word[0] + word[1] + "ay" + '\n'

        elif word ==' ':
            return ' '

        elif word[0]=='ABCDEFGHIJKLMNOPQRSTUVWXYZ':
            return word + ' ' + '-->' + ' ' + word[1:] + word[1].upper() + word[0].lower() + 'ay' + '\n'

def main():
    x = raw_input('Please give a name of a file: ')
    inputfile = open(x,'r')
    outputfile = open(x + 'piglat.txt','w')
    line = inputfile.readline()
    while line!='':
        print piglatin(line)
        outputfile.write(piglatin(line))
        line=inputfile.readline()

    inputfile.close()
    outputfile.close()

main()



Look at all those return statements! The moment you hit one, it'll end the function! instead of using return, just throw the value into a list and then return the list at the end.

Also, some of this code doesn't seem to make any sense. For example, this line says elif word[0]=='ABCDEFGHIJKLMNOPQRSTUVWXYZ' that if the first letter of the word is equal to the entire uppercase alphabet... which is impossible. Surely that wasn't your friend's intention. He probably meant to say elif word[0] in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

He should put a comment above each condition explaining the purpose of it so that we'll understand what he was thinking. Good commenting is vital to being a good programmer!

This post has been edited by atraub: 07 June 2011 - 03:39 AM

Was This Post Helpful? 2
  • +
  • -

#3 itdoell   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 239
  • Joined: 13-January 11

Re: Python script only reading first word of each line.

Posted 07 June 2011 - 12:11 AM

Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1