Issue
I am trying to make a pig latin function in Python, which takes in a word and returns its pig latin translation. You can convert an English word into Pig Latin by moving all the consonants up to the first vowel to the end of the word and then appending "ay", e.g 'hello' becomes 'ellohay'. This is my code:
def pig_latin(word):
""" Returns the pig latin translation of a word. """
i=0
new_word = word
vowel = ["a", "e","i","o","u"]
while word[i] not in vowel:
word[i] = consonant
new_word = new_word[(i+1):] + consonant
i+=1
final_word = new_word + "ay"
return final_word
When I run it, it gives:
NameError: name 'consonant' is not defined
I would really appreciate help regarding this.
Thank you!
Solution
Inside your while loop you have word[i] = consonant
. Since that's the first use of consonant
, it can't be on the right-hand side of an assignment. You just need to switch that around to work.
consonant = word[i]
(Your function seems to work with words that begin with consonants, but test it out with words that begin with vowels. I'm not sure what the rules of pig latin are.)
Answered By - Bill the Lizard
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.