简体   繁体   English

变量未定义 PYTHON 3.5.2

[英]Variable not defined PYTHON 3.5.2

def GameMode():#creates a function with name of gamemode
    global wrong_answer_1
    for keys,values in keywords.items():#checks for the right answer
        if keys == code:#if the keys equal to the code value
            keyword_c = values
            global keyword_c
    for keys,values in definition.items():#checks for the right answer
        if keys == code + 1:#if the keys equal the code add 1
            definition_c = values#set s to be the values
    for keys,values in definition.items():#checks for the right answer
        if inc == keys:#if the keys equal the code add 1
            wrong_answer_1 = values#set s to be the values
    for keys,value in definition.items():#For the keys in the dictionary
        if keys == inc2:#if the keys equal to a value
            global wrong_answer_2
            wrong_answer_2 = value

    print(wrong_answer_2, "Hi")

I am trying to get my variables keyword_c, definition_c, wrong_answer_1 and wrong_answer_2 to be global so I can use them in another function but I can't seem to get it to work, I am a bit of a newb when it comes to python.我试图让我的变量 keyword_c、definition_c、error_answer_1 和 wrong_answer_2 成为全局变量,所以我可以在另一个函数中使用它们,但我似乎无法让它工作,当谈到 python 时,我有点新手。 I've tried using "global" as you can see above and I have tried to call variables and pass them but I don't fully understand it enough to be able to figure it out.正如您在上面看到的那样,我尝试使用“全局”,并尝试调用变量并传递它们,但我对它的理解还不够充分,无法弄清楚。

keyword_c = ''
definition_c = ''
wrong_answer_1 = ''
wrong_answer_2 = ''

I have already predefined the variables as well to see if that did anything, they are there but its now just an empty string so my program is picking up the fact that the variable is being defined.我也已经预定义了变量,看看是否有任何作用,它们在那里,但它现在只是一个空字符串,所以我的程序正在获取正在定义变量的事实。

Traceback (most recent call last):
  File "D:\Program.py", line 67, in <module>
   GameMode()#calls the function
  File "D:\Program.py", line 55, in GameMode
    print(wrong_answer_2, "Hi")
NameError: name 'wrong_answer_2' is not defined

here is the error I get if I remove the original line that sets it as a blank string如果我删除将其设置为空白字符串的原始行,这是我得到的错误

Why didn't you want to create a class with variables:你为什么不想创建一个带有变量的类:

self.keyword_c = ''
self.definition_c = ''
self.wrong_answer_1 = ''
self.wrong_answer_2 = ''

so variables would be global (if every method and variable is inside class) and with your method:所以变量将是全局的(如果每个方法和变量都在类中)并且使用您的方法:

def GameMode(self):#creates a function with name of gamemode
    for keys,values in keywords.items():#checks for the right answer
        if keys == code:#if the keys equal to the code value
            self.keyword_c = values
            #(...)

And actually, here is a mistake which might produce error(commented it):实际上,这是一个可能会产生错误的错误(评论它):

def GameMode():#creates a function with name of gamemode
    global wrong_answer_1
    for keys,values in keywords.items():#checks for the right answer
        if keys == code:#if the keys equal to the code value
            keyword_c = values # keyword_c doesn't exist here, you cannot assign here
            global keyword_c   # its created here, so it now exists

I am not sure what your function is supposed to be doing;我不确定你的函数应该做什么; it is hard to guess with all the syntax errors, but the following fixes the syntax errors, using a single global at the top of the function for everything which is not defined within the function.很难猜测所有的语法错误,但以下修复了语法错误,在函数顶部使用单个全局函数来处理未在函数中定义的所有内容。

def GameMode():#creates a function with name of gamemode
    global inc, inc2, code, wrong_answer_1, wrong_answer_2, keyword_c
    for keys,values in keywords.items():#checks for the right answer
        if keys == code:#if the keys equal to the code value
            keyword_c = values
    for keys,values in definition.items():#checks for the right answer
        if keys == code + 1:#if the keys equal the code add 1
            definition_c = values#set s to be the values
    for keys,values in definition.items():#checks for the right answer
        if inc == keys:#if the keys equal the code add 1
            wrong_answer_1 = values#set s to be the values
    for keys,value in definition.items():#For the keys in the dictionary
        if keys == inc2:#if the keys equal to a value
            wrong_answer_2 = value

    print(wrong_answer_2, "Hi")

keywords = {
    1: 'a',
}
definition = {
    1: 'a',
}
code = 1
inc = 1
inc2 = 1
wrong_answer_1 = None
wrong_answer_2 = None
keyword_c = None

GameMode()
print(inc, inc2, code, wrong_answer_1, wrong_answer_2, keyword_c)

That will output:这将输出:

a Hi
1 1 1 a a a

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

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