1

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?

11
  • 1
    Why not pass them as parameters? Commented Dec 30, 2017 at 19:15
  • There are couple of problems with your program: you never launch the method easyHistoryQuestions. Inside of the body of that function anyway, you'll ask the user for input but you will just overwrite that answer each time. Then the other function will never access to these values as there are not global. I'd suggest for the moment to just do this without any function and then try to refactor with functions once you get the hang of it Commented Dec 30, 2017 at 19:15
  • @StephenRauch I thought you could only pass in 2 parameters? Commented Dec 30, 2017 at 19:20
  • There is no practical limit to the number of parameters. Commented Dec 30, 2017 at 19:21
  • @Axnyff I did call the function in my actual program I just didn't include it in this code. I'm not sure what you mean by I will be overwriting my results? How would that happen inside the function if I have never mentioned the variable name again except when checking it (==). I understand that they're not global but I'm wondering how I can make them global? I'm trying to learn Python so I'd love to learn how to use functions and global variables properly instead of just skip over it. Thanks for your suggestions though. Commented Dec 30, 2017 at 19:26

2 Answers 2

2

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 (i.e. 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)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

Why would you recommend this?
But if I said for example "hisAnswer1E = 0" outside of the function, wouldn't that change my answer? It would then result in all of my answers being incorrect if they were actually right
@StephenRauch I'm not actually recommending the usage of global variables, I was just trying to explain how to use them in python. I edited my answer. Thank you!
@SClarke whenever a global variable is modified, may it be from the inside or the outside of a function, it is modified "everywhere", so yes you're right. That's one of the reasons why using a global variable is tricky and may cause a lot of unwanted bugs and complicates a lot of things. The answer of zvone gives a simple, less troublesome way of achieving what you want, avoiding the use of a global variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.