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")
我正在尝试将变量keyword_c,definition_c,wrong_answer_1和wrong_answer_2设为全局变量,因此我可以在其他函数中使用它们,但我似乎无法使其工作,我有点像新手一样它来到python。 我尝试过使用" global"正如你在上面看到的那样,我试图调用变量并传递它们,但我不能完全理解它,以便能够弄明白。
keyword_c = ''
definition_c = ''
wrong_answer_1 = ''
wrong_answer_2 = ''
我已经预先定义了变量,看看它是否有效,它们在那里,但它现在只是一个空字符串,所以我的程序正在接受变量被定义的事实。
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
如果我删除将其设置为空字符串的原始行
,则会出现错误答案 0 :(得分:0)
为什么不想创建包含变量的类:
self.keyword_c = ''
self.definition_c = ''
self.wrong_answer_1 = ''
self.wrong_answer_2 = ''
因此变量将是全局的(如果每个方法和变量都在类中)并且使用您的方法:
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
#(...)
实际上,这是一个可能产生错误的错误(评论它):
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
答案 1 :(得分:0)
我不确定你的功能应该做什么;很难用所有的语法错误来猜测,但是下面修复了语法错误,使用函数顶部的单个全局函数来处理函数中未定义的所有内容。
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)
那将输出:
a Hi
1 1 1 a a a