2016-09-29 96 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