Python Forum

Full Version: NameError: Global Name is not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I compiled the following first steps of a quiz:

easy_text = '''The Simpsons is an animated sitcom created by ___1___ .
The father of the family is called ___2___ and the mother's name is ___3___ .
Both have a naughty son who is called ___4___ .'''
list_of_easy_text = easy_text.split()
#list with correct answers in easy_text:
list_answers_easy_text = ['Matt Groening', 'Homer', 'Marge', 'Bart']

#User is asked to fill in blanks in easy_text:
def play_easy_text():
    user_answer_easy_test_1 = raw_input("Your answer for ___1___:")
    print user_answer_easy_test_1
    check_correct_easy_text()
    play_easy_text()

def check_correct_easy_text():
    if user_answer_easy_test_1 == list_answers_easy_text[0]:
        print "Your answer is correct!"
    else:
        print "Your answer is wrong. Try it again!"
        check_correct_easy_text()

def determination_level():
    level = raw_input("Choose: easy, medium, hard.")
    if level == "easy":
        print "You have chosen the level 'easy'."
        print easy_text
        play_easy_text()
    elif level == "medium":
        print "You have chosen the level 'medium'."
    elif level == "hard":
        print "You have chosen the level 'hard'."
    else:
        print "Wrong input!"
        determination_level()
determination_level()
When I run this code, I get the following error:
NameError: global name 'user_answer_easy_test_1' is not defined

Can you help me to solve this problem? Thanks a lot in advance!
When you define a variable in a function, it is only accessible in that function. So the user_answer_easy_test_1 you defined in play_easy_text is not available in check_correct_easy_text.

To make information available to other functions you pass it out of one function with the return statement, possibly assigning it to a variable. Then you pass it into another function using a parameter. See the functions tutorial link in my signature for a detailed explanation.
that information is very helpful, thanks a lot ichabod801 !!!
Next time you just seek for = in functions.
If the name is identical to a global, you found the error.