Hi, I am doing an online tutorial for python on codeacademy and i already created a functional pyg latin translator that uses raw_input and turns it into a word in pyglatin, however, when I try to turn this translator into a function that takes a word and returns a word in pyg latin I get an error. Is there a fundamental difference in the way these work?
Here is the functional translator:
original = raw_input("Enter a word in English to translate to Pyg Latin:")
vowels = ["a", "e", "i", "o", "u"]
if len(original) > 0 and original.isalpha():
word = original.lower()
if word[0] in vowels:
translation = word + "ay"
print translation
else:
translation = word[1:] + word[0] + "ay"
print translation
else:
print "This is not a valid entry! Please try again."
# Here is the function that comes up with an error:
vowels = ["a", "e", "i", "o", "u"]
def pyglatin(eng):
if eng.isalpha() and len(eng) > 0:
word = eng.lower()
if word[0] in vowels:
return word + "ay"
else:
return word[1:] + word[0] + "ay"
else:
return False
When I try and call the function and type pyglatin(ant) for example to see the translation of the word ant, I get this error:
Traceback (most recent call last):
File "", line 1, in pyglatin(ant) NameError: name 'ant' is not defined
Please note that all of the indenting is correct, but I may not have shown the correct spacing here. I really just want to know if there's a fundamental problem with my logic. Thanks!!!
vowels = ["a", "e", "i", "o", "u"]can be replaced withvowels = "aeiou"since each list item is just one character long.len(eng) > 0can be replaced withengsince a string is truthy if its length is greater than zero and falsy otherwise.eng.isalpha()returns false if the string is empty, so that's the only test needed there.ifstatement. But that doesn't matter, becauseeng.isalpha()really does both those tests for you anyway, so they do the same thing. It would help to see how you call the function, of course. Can you provide a complete example?