简体   繁体   中英

Trouble with global variable names and different functions (using Python)

I am trying to program a History quiz of different difficulties using functions but have run into some trouble regarding "global names". I have tried to correct this but nothing seems to be working.

The snippet code is:

#Checking Answers
def checkEasyHistoryQuestions():
    score = 0
    if hisAnswer1E == 'B' or hisAnswer1E == 'b':
        score = score + 1
        print "Correct!"
    else:
        print "Incorrect!"

    if hisAnswer2E == 'A' or hisAnswer2E == 'a':
        score = score + 1
        print "Correct!"
    else:
        print "Incorrect!"

    if hisAnswer3E == 'B' or hisAnswer3E == 'b':
        score = score + 1
        print "Correct!"
    else:
        print "Incorrect!"

    print score
    print "\n"

#History - Easy - QUESTIONS + INPUT 
def easyHistoryQuestions():
    print "\n"
    print "1. What date did World War II start?"
    print "Is it:", "\n", "A: 20th October 1939", "\n", "B: 1st September 1939"
    hisAnswer1E = raw_input("Enter your choice: ")
    print "\n"

    print "2. When did the Battle of Britain take place?"
    print "Is it: ", "\n", "A: 10th July 1940 – 31st October 1940", "\n", "B: 3rd July 1940- 2nd August 1940" 
    hisAnswer2E = raw_input("Enter your choice: ")
    print "\n"

    print "3. Who succeeded Elizabeth I on the English throne?"
    print "Is it: ", "\n", "A. Henry VIII", "\n", "B. James VI"
    hisAnswer3E = raw_input("Enter your choice: ")
    print "\n"

checkEasyHistoryQuestions()

The error I'm getting is:

if hisAnswer1E == 'B' or hisAnswer1E == 'b':
NameError: global name 'hisAnswer1E' is not defined

I have tried to declare hisAnswer1E as a global variable within the function aswell as outside it.

For example:

print "\n"
print "1. What date did World War II start?"
print "Is it:", "\n", "A: 20th October 1939", "\n", "B: 1st September 1939"
global hisAnswer1E 
hisAnswer1E = raw_input("Enter your choice: ")
print "\n"  

And also:

global hisAnswer1E 

#Checking Answers
def checkEasyHistoryQuestions():
    score = 0
    if hisAnswer1E == 'B' or hisAnswer1E == 'b':
        score = score + 1
        print "Correct!"
    else:
        print "Incorrect!"

Nothing seems to be working and I just keep getting the same error. Any ideas why?

Declaring global hisAnswer1E means "use hisAnswer1E from global scope" . It does not actually create a hisAnswer1E in the global scope.

So, to use a global variable, you should first create a variable in the global scope and then declare global hisAnswer1E in a function

HOWEVER !!!

A piece of advice: don't use global variables. Just don't.

Functions take arguments and return values. That is the correct mechanism to share data between them.

In your case, the simplest solution (ie least changes to what you did so far) is to return the answers from easyHistoryQuestions and pass them to checkEasyHistoryQuestions , for example:

def checkEasyHistoryQuestions(hisAnswer1E, hisAnswer2E, hisAnswer3E):
    # <your code here>

def easyHistoryQuestions():
    # <your code here>
    return hisAnswer1E, hisAnswer2E, hisAnswer3E

hisAnswer1E, hisAnswer2E, hisAnswer3E = easyHistoryQuestions()
checkEasyHistoryQuestions(hisAnswer1E, hisAnswer2E, hisAnswer3E)

To use a global variable in python, I think you should declare the variable without the global keyword outside of the function, and then declare it with the global keyword inside of the function.

x=5
def h():
   global x
   x=6

print(x)
h()
print(x)

This code would print

5
6

However global variables are usually something to avoid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM