0

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!!!

5
  • 4
    What error are you getting? Post the full traceback. Your function looks good, so it's probably somewhere else in your code. Commented Apr 27, 2013 at 5:57
  • 2
    It probably won't fix your problem, but a few suggestions: vowels = ["a", "e", "i", "o", "u"] can be replaced with vowels = "aeiou" since each list item is just one character long. len(eng) > 0 can be replaced with eng since a string is truthy if its length is greater than zero and falsy otherwise. Commented Apr 27, 2013 at 6:01
  • 4
    @icktoofay - it's even simpler than that: eng.isalpha() returns false if the string is empty, so that's the only test needed there. Commented Apr 27, 2013 at 6:22
  • 2
    The only real difference between the function version and the original code is the order of the tests in the first if statement. But that doesn't matter, because eng.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? Commented Apr 27, 2013 at 6:24
  • Thanks for the help. Here's the error to be more specific. 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 "<pyshell#0>", line 1, in <module> pyglatin(ant) NameError: name 'ant' is not defined Commented Apr 28, 2013 at 18:23

2 Answers 2

2

File "", line 1, in pyglatin(ant) NameError: name 'ant' is not defined

pyglatin(ant) means run it on the variable ant, which is undefined. To pass in a literal string, use quotes:

pyglatin('ant')

There are many more ways to represent literal strings in Python, but this is the simplest and most obvious.

Sign up to request clarification or add additional context in comments.

Comments

0

It is hard to know, without knowing what the error is that you are getting. Though perhaps: could the problem be that in the global scope of your program you are not assigning the return ... to anything? What do I mean by this? An example:

def hello():
    return 'Hello, world!'

hello()

output: there is nothing to output in this case, because you have not provided any measn to reference the return value of hello(). But, if you were to do the following:

print(hello())
--> Hello, world!

greeting = hello()
print(greeting)
--> Hello, world!

The first example, print the return statement from hello(), and the second assigns the return value to a variable, giving you a means to reference it again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.