1 Replies - 1047 Views - Last Post: 23 February 2013 - 07:48 AM Rate Topic: -----

#1 ffxfan67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-February 13

Writing Pig Latin program

Posted 21 February 2013 - 04:06 PM

I'm trying to write a code that processes a text document and "pig-latinizes" the words with in the text.

My skeleton looks like this


def convertWord(aWord):
    # define vowels
    vowel = "aeiouAEIOU"

    pigWord = ""
    beginsWithVowel = aWord[0] in vowel
    hasPunctuation = not aWord[-1].isalnum()

    if vowel==[0]:
        aWord+=[0]+'yay'
    else:
        aWord+='ay'

    return pigWord
# end convertWord




My book says to use slicing and create a string of vowels with 'aeiou'.

I have tried several problems. My main issue is getting the text from my document (we'll call it 'Text.txt') to convert each word.

I used

if vowel==[0]:
    aWord=[0]+'yay'
else:
    aWord+='ay'



My Python program says "working...."

But the output file has nothing. None of my exercises showed me how to exactly bring in text from another document and process it and my professor lightly touched on it.

Is This A Good Question/Topic? 0
  • +

Replies To: Writing Pig Latin program

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 62
  • View blog
  • Posts: 695
  • Joined: 19-January 09

Re: Writing Pig Latin program

Posted 23 February 2013 - 07:48 AM

Hi,
if you are just looking for some help opening your text file and splitting it up, you could do something like this:
f = open('text.txt','r') #open the file
read = f.read() #read it's contents

word_list = read.split(' ') #split up the words when ever there is a space
f.close()


after this you can use a for loop to go through each of the words in word_list.

Also, lines 9 and 10 of your function look to be incorrect to me, I am not sure what you are comparing your variable vowel to? It looks like you are saying if vowel is equal to a list with one value of zero in it, then do.... Is that what you wanted? I could recode this for you, but I will let you attempt to fix the problem first!

I hope that helps!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1