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、error_answer_1 和 wrong_answer_2 是全局的,所以我可以在另一个函数中使用它们,但我似乎无法让它工作,当谈到 python 时,我有点新手。如上所示,我尝试使用“全局”,并且尝试调用变量并传递它们,但我对它的理解还不够充分,无法弄清楚。
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
如果我删除将其设置为空白字符串的原始行,这是我得到的错误