1 Replies - 1174 Views - Last Post: 27 March 2012 - 09:22 AM Rate Topic: -----

#1 ssharma286  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 93
  • Joined: 04-December 10

Pig Latin Translator Loop Error

Posted 22 March 2012 - 05:19 PM

Hello I have this weird error. I am new to python coming from a C/C++ background. This program makes a pig latin translation. I am having problems with punctuations like , and !. My algorithm is that I divide a given text into words then I send those words through a function. Now in my function go through each letter and collects the punctuations into a variable and it is supposed to add all those punctuations when the translation is done. Now for some reason this loop that does absolutely nothing! destroys the entire answer. Any Ideas as to whats going on

Output with the loop:
Piglatin sucks
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- Npiglatiay --- --- --- --- --- --- --- --- --- --- ssuckay 


Output without loop
Piglatin sucks
Iglatinpay uckssay 


def validLetter(letter):
    letter = letter.lower()
    if(ord(letter) >= 97 and ord(letter) <= 122):
        return True
    return False
def isVowel(letter):
    letter = letter.lower()
    if(letter == 'a' or letter == 'e' or letter ==  'i' or letter == 'o' or letter == 'u' or letter == 'y'):

        return True
    return False
def translate(word):
    if(word != ''):
        if(isVowel(word[0])):
           word += 'way'
           return word
        elif(not isVowel(word[0])):
            wasCap = word[0].isupper()
            word = word.lower()
            extras = '' #collects the punctuation
            for index, letter in enumerate(word):
                if(isVowel(letter)):
                    break;
           # for index,letter in enumerate(word): <----- for some reason this loop destroys everything! 
           #     print('hi',end = ' ') 
            word = word[index:] + word[0:index] + 'ay'
            if(wasCap): word =  word[0].upper() + word[1:]
            return word
        else: return None


while True:
    text = (input()).split()
    for word in text:
       if(translate(word) != None): print(translate(word), end = ' ')
 

This post has been edited by ssharma286: 22 March 2012 - 05:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pig Latin Translator Loop Error

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Pig Latin Translator Loop Error

Posted 27 March 2012 - 09:22 AM

Hmmm, what do you hope that loop will do?
for index,letter in enumerate(word):
    print('hi',end = ' ') 


all it does is print out 'hi' a lot...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1