简体   繁体   English

全局变量名称和不同函数的问题(使用Python)

[英]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. 我试图将hisAnswer1E声明为函数内部以及外部的全局变量。

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" . 声明global hisAnswer1E意味着“从全局范围使用hisAnswer1E It does not actually create a hisAnswer1E in the global scope. 实际上,它不会在全局范围内创建hisAnswer1E

So, to use a global variable, you should first create a variable in the global scope and then declare global hisAnswer1E in a function 因此,要使用全局变量,您应该首先在全局范围内创建一个变量,然后在函数中声明global hisAnswer1E

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: 在您的情况下,最简单的解决方案(即至少对到目前为止所做的更改)是从easyHistoryQuestions返回答案并将其传递给checkEasyHistoryQuestions ,例如:

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. 要在python中使用全局变量,我认为您应该在函数外部声明没有 global关键字的变量,然后在函数内部 使用 global关键字声明它。

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. 但是,通常应避免使用全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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