【问题标题】:Variable not defined PYTHON 3.5.2变量未定义 PYTHON 3.5.2
【发布时间】:2017-02-07 11:56:46
【问题描述】:
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 成为全局变量,以便我可以在另一个函数中使用它们,但我似乎无法让它工作,当它出现时我有点新手到蟒蛇。 如上所示,我尝试使用“全局”,并且尝试调用变量并传递它们,但我对它的理解不够充分,无法弄清楚。

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

如果我删除将其设置为空白字符串的原始行,则会出现以下错误

【问题讨论】:

  • 将变量设为全局“以便我可以在另一个函数中使用它们”几乎总是是错误的做法。相反,从这个函数返回它们并将它们传递给另一个。
  • 另外,当询问有关错误的问题时,请务必发布完整的错误和回溯。
  • 啊,我明白了,我真的不明白该怎么做,你能告诉我如何正确传递它们吗?
  • 仅供参考 #creates a function with name of gamemode确切地说那种不应该出现在你的代码中的毫无价值的评论。不要描述代码做了什么,而是描述为什么这样做。另外,使用文档字符串。
  • 对不起,他们并没有真正解决我自己的问题,我不知道文档字符串是什么或如何使用它们,你能提供一个链接到可以很好解释的东西吗?跨度>

标签: python variables global-scope python-3.5


【解决方案1】:

你为什么不想创建一个带变量的类:

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

【讨论】:

  • 请记住,如果if 语句都不是True,那么keyword_cwrong_answer_2 将不存在,因为它们的创建在if 语句中。这就是为什么NameError: name 'wrong_answer_2' is not defined 显示
【解决方案2】:

我不确定你的函数应该做什么;很难猜测所有语法错误,但以下修复了语法错误,在函数顶部使用单个全局函数来处理函数中未定义的所有内容。

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

【讨论】:

  • 我已经完成了你所说的,但是每当我尝试打印变量时,我仍然会得到一个空白字符串。也许我只是烂>:
  • 那是和原来的问题不同的问题。我们需要知道您的输入是什么以及预期的输出,以帮助创建一个算法来做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 2011-04-29
  • 2018-11-21
  • 1970-01-01
  • 2021-10-09
相关资源
最近更新 更多