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时,我有些新鲜感。 我试过使用“全局”,因为你可以看到上面,我试图调用变量并通过它们,但我不完全理解它足以能够弄清楚。变量没有定义PYTHON 3.5.2
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
这里是我的错误,如果我删除其设置为空字符串原线
决策变量的全局“这样我就可以在另一个函数中使用它们”是*几乎总是*错误的事情。相反,从这个函数返回它们并将它们传递给另一个函数。 –
另外,当询问有关错误的问题时,请始终发布完整的错误和回溯。 –
啊,我看到了,我真的不明白怎么做,你能告诉我如何正确地传递它们吗? – BushellsMaster