user2326053
user2326053

Reputation:

Beginner debugging a function

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

Upvotes: 0

Views: 373

Answers (2)

tripleee
tripleee

Reputation: 189387

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.

Upvotes: 2

Nick Burns
Nick Burns

Reputation: 983

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.

Upvotes: 0

Related Questions