mardi 21 août 2018

if statement: NameError: Global Name not defined

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_text_1 = raw_input("Your answer for ___1___:")
    print user_answer_easy_test_1
    check_correct_easy_text()
    user_answer_easy_text_2 = raw_input("Your answer for ___2___:")
    print user_answer_easy_text_2
    user_answer_easy_text_3 = raw_input("Your answer for ___3___:")
    print user_answer_easy_text_3
    user_answer_easy_text_4 = raw_input("Your answer for ___4___:")
    print user_answer_easy_text_4

#Check if user-answer for easy_text is correct
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()

If I run this code, I get the following error:

Traceback (most recent call last):
  File "my-project-fill-blanks.py", line 68, in <module>
    determination_level()
  File "my-project-fill-blanks.py", line 56, in determination_level
    play_easy_text()
  File "my-project-fill-blanks.py", line 33, in play_easy_text
    check_correct_easy_text()
  File "my-project-fill-blanks.py", line 43, in check_correct_easy_text
    if user_answer_easy_test_1 == list_answers_easy_text[0]:
NameError: global name 'user_answer_easy_test_1' is not defined

But for my understanding, I already defined the variable 'user_answer_easy_test_1' in play_easy_text() What is wrong with the code?

Aucun commentaire:

Enregistrer un commentaire