簡體   English   中英

變量未定義 PYTHON 3.5.2

[英]Variable not defined PYTHON 3.5.2

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

如果我刪除將其設置為空白字符串的原始行,這是我得到的錯誤

你為什么不想創建一個帶有變量的類:

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

我不確定你的函數應該做什么; 很難猜測所有的語法錯誤,但以下修復了語法錯誤,在函數頂部使用單個全局函數來處理未在函數中定義的所有內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM