Python Forum
NameError: Global Name is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: Global Name is not defined
#1
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!
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
that information is very helpful, thanks a lot ichabod801 !!!
Reply
#4
Next time you just seek for = in functions.
If the name is identical to a global, you found the error.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 173 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,057 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,776 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,205 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,457 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,865 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,186 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,678 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,306 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,845 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020