0

I am trying to write a code which will turn a word into pig latin/dog latin. For example, is will become isyay, scram will become amscray. The first situation works, however, the second situation prints cramsay. Why does it not seem to be looping properly?

def doggify(word):
    wordlist=list(word)
    start=word[0]
    if start=='a' or start=='e' or start=='i' or start=='o' or start=='u':
       dogword=word+'yay'
       return print(dogword)
    else:
       empty=[]
       for letter in wordlist:
            if letter!='a' or letter!='e' or letter!='i' or letter!='o' or letter!='u':
               empty.append(letter)
               wordlist.remove(letter)
            dogword=''.join(wordlist)+''.join(empty)+'ay'
            return print(dogword)
Hannah
  • 165
  • 2
  • 3
  • 13

3 Answers3

1

What you should do is actually add the last two lines inside an else statement, and use another list for the remaining original letters. Since you're removing from the list you're iterating over, letters are being skipped. This code actually does it:

def doggify(word):
    wordlist = list(word)
    start = word[0]
    if start in 'aeiou':
        dogword = word + 'yay'
        return dogword
    else:
        empty = []
        new = list(wordlist)
        for letter in wordlist:
            if letter not in 'aeiou':
                empty.append(letter)
                new.remove(letter)
            else:
                dogword = ''.join(new) + ''.join(empty) + 'ay'
                return dogword
sbacarob
  • 2,092
  • 1
  • 14
  • 19
  • Tried it. Same problem. It still prints "cramsay" for a word like scram. – Hannah Feb 17 '17 at 06:14
  • This actually works. I was overlooking the important things, specially that you were removing from the list you were iterating over – sbacarob Feb 17 '17 at 06:26
  • Thank you so much! Do you happen to know why the second else statement is needed? Or why it doesn't seem to work if I do letter !='a'...etc.? – Hannah Feb 17 '17 at 06:36
  • 1
    The else block is needed because without it, that code gets read after the if anyway and returns on the first iteration of the loop. You just need to return when you find a vowel, which is when you move the previously found consonants to the end and add 'ay'. letter != 'a' and letter != 'b'... etc should work. But notice that in this case you MUST use and instead of or, since you are checking that the letter isn't any of the vowels. When you're checking start, however, you must use or, since if the first letter is any of the vowels, that code should be executed. – sbacarob Feb 17 '17 at 06:39
0

Please try the following code:-

def doggify(word):
 wordlist=list(word)
 start=word[0]
 if start=='a' or start=='e' or start=='i' or start=='o' or   start=='u':
   dogword=word+'yay'
   return dogword
 else:
    empty=[]
    for letter in wordlist:
        if letter!='a' or letter!='e' or letter!='i' or letter!='o' or letter!='u':
         empty.append(letter)
         wordlist.remove(letter)
         dogword=''.join(wordlist)+''.join(empty)+'ay'
         return dogword

input:- print doggify('test')
output:- esttay
Hilar AK
  • 1,655
  • 13
  • 25
0
def doggify(word):
    if word[0] in 'aeiou':
       return word+'yay'
    else:
       consonants = []
       tail = []
       out = consonants
       for letter in word:
            if letter in 'aeiou' and out is not tail:
                out = tail
            out.append(letter)
       return ''.join(tail)+''.join(consonants)+'ay'

print(doggify('scram'))

prints amscray

Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
  • Yes, this is close, however the desired output is amscray. I basically want the consonants to be appended to empty until a vowel is reached. Then I only want wordlist to contain what is left – Hannah Feb 17 '17 at 06:30
  • Corrected. You could've written it in the question, though ;) – Antony Hatchkins Feb 17 '17 at 06:41
  • Haha, you are correct. I clearly shouldn't be posting on stack exchange at 2am! – Hannah Feb 17 '17 at 17:07