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