2016-09-29 88 views
-1
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 

這裏是我的錯誤,如果我刪除其設置爲空字符串原線

+3

決策變量的全局「這樣我就可以在另一個函數中使用它們」是*幾乎總是*錯誤的事情。相反,從這個函數返回它們並將它們傳遞給另一個函數。 –

+1

另外,當詢問有關錯誤的問題時,請始終發佈完整的錯誤和回溯。 –

+0

啊,我看到了,我真的不明白怎麼做,你能告訴我如何正確地傳遞它們嗎? – BushellsMaster

回答

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 
+0

請記住,如果if語句都不是'True',那麼'keyword_c'和'wrong_answer_2'將不存在,因爲它們的創建在'if'語句中。這就是爲什麼'NameError:name'wrong_answer_2'沒有被定義'顯示 –

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 
+0

我已經完成了你所說的內容,但是當我嘗試打印變量時,我仍然收到一個空白字符串。也許我只是吮吸>: – BushellsMaster

+0

這是與原來的問題不同的問題。我們需要知道你的輸入是什麼,以及預期的輸出,以幫助創建一個算法來完成它。 –

+0

我已經修復了它,現在感謝xxoxooxox – BushellsMaster