2016-09-29 5 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になると少し新鮮です。 私は上記のように "global"を使ってみましたが、変数を呼び出して渡すことを試みましたが、それを理解するのに十分理解していません。 変数は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'は存在しません。それで、 '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