1

I am currently working on my Pig Latin code in python. I am encountering two errors, the first one is dealing with consonants. If a word begins with two consonants, both letters are moved to the end and 'ay' is appended: grade becomes to adegray for instance.

My second issue is, that I get invalid syntax error, when I try to run my code below:

#Pig latin

pig= ("ay")
word = input("Enter a word: ")# prompt for a phrase and assign result to a variable
vowels="aeiou" # list of vowels

words = word.split()
# for word in words
for i in range(len(word)):# assign first letter of phrase to a string variable, for later use
    if word[i][0] in vowels: # is first letter a vowel 
        print(word[i] + 'way') # if first letter is a vowel, print word + 'way'
    elif word[i][1] in vowels:
        print(word[i][1:]+word[i][0] + 'ay' # assign second letter of phrase to a string variable, for later use
    else:
        print(word[i]+ [1:]=('ay') # otherwise print word with first two letters moved to end & added 'ay'
clemens
  • 16,716
  • 11
  • 50
  • 65

1 Answers1

0
#Pig latin

pig= ("ay")
word = raw_input("Enter a word: ")# prompt for a phrase and assign result to a variable
vowels="aeiou" # list of vowels

words = word.split()

for i in range(len(words)):
    if words[i][0] in vowels: # is first letter a vowel 
        print words[i] + 'way', # if first letter is a vowel, print word + 'way'
    elif words[i][1] in vowels:
        print words[i][1:] + word[i][0] + 'ay', # assign second letter of phrase to a string variable, for later use
    else:
        print words[i][2:] + words[i][0:2] + 'ay', # otherwise print word with first two letters moved to end & added 'ay'

Some notes on what I fixed:

  1. Your syntax error was due to an unclosed parenthesis

  2. There was some weird stuff going on with your for loop, you had word in some places you should have had words.

  3. I was able to use some more list slicing to finish handling the case where the word starts with two vowels

  4. I added commas at the end of the print statements so that there is no newline after each word (it prints all the words on one line, just the way they were inputed)

  5. input() is for numbers I beleive, what you wanted was raw_input()

setholopolus
  • 253
  • 3
  • 17
  • Thank you @setholopolus, I got it to work using your suggestions and fixes, for some reason *raw_input()* comes back with an error, I was able to get it to work by just using input(). – Okeine Hermit Nov 21 '17 at 19:19
  • Ah, you must be using Python 3. I was using 2.7. For 2.7, what I said is (mostly) true about `input()` vs. `raw_input()` See here: https://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x – setholopolus Nov 21 '17 at 21:03