0

So I am writing a program that asks for your input, puts the words of the sentence you entered into a list and makes the words in the statement go trough the while loop one by one.

The while loops works as follows: if the first letter of a word is a Vowel it print the word + hay. Ifthe first letter of the word is not a vowel it puts the first letter of the word at the end of the word + ay

the code:

VOWELS = ['a','e','i','o','u']

def pig_latin(phrase):
    #We make sure the input is changed in only lower case letters.
    #The words in your sentence are also putted into a list
    lowercase_phrase = phrase.lower()
    word_list = lowercase_phrase.split()
    print word_list

    x = 0

    while x < len(word_list):

        word = word_list[x]
        if word[0] in VOWELS:
            print word + 'hay'

        else:
            print word[1:] + word[0] + 'ay'
        x = x+1

pig_latin(raw_input('Enter the sentence you want to translate to Pig Latin, do not use punctation and numbers please.'))

My problem: If i enter for example: "Hello my name is John" in the_raw input at the end of the code i will get the following output:

ellohay
ymay
amenay
ishay
ohnjay

But i actually want the following output:

ellohay ymay amenay ishay ohnjay

If someone could explain me how to achieve this output it would be appriciated

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

2

Save the new words in another list, then at the end:

print(" ".join(pig_latin_words))

Example:

VOWELS = {'a','e','i','o','u'}

def pig_latin(phrase):
    #We make sure the input is changed in only lower case letters.
    #The words in your sentence are also putted into a list
    word_list = phrase.lower().split()
    print(word_list)

    pig_latin_words = []

    for word in word_list:
        if word[0] in VOWELS:
            pig_latin_words.append(word + "hay")
        else:
            pig_latin_words.append(word[1:] + word[0] + "ay")

    pig_latin_phrase = " ".join(pig_latin_words)

    print(pig_latin_phrase)

    return pig_latin_phrase
Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
0

Append a comma (,) at the end of your print statements to avoid the newline:

while x < len(word_list):

    word = word_list[x]
    if word[0] in VOWELS:
        print word + 'hay',

    else:
        print word[1:] + word[0] + 'ay',
    x = x+1
arturomp
  • 28,790
  • 10
  • 43
  • 72