2017-05-01 3 views
0

に非ローカル使用カント:Python-グローバルを使用しています。これは私のコードであるPythonのタイピングゲーム

enter code here 
def Game(): 
try: 

    tkMessageBox.showinfo("Hello!", "Welcome To my Game, %name") 
    root.destroy() 

    colors = ['Red', 'Blue', 'Green', 'Pink', 'Orange', 'Black', 'Yellow', 
    'White', 'Purple', 'Brown'] 
    score = 0 
    timeLeft = 30 

    def startGame(event): 
     global timeLeft 
     if timeLeft == 30: 
      countdown() 

     nextColor() 

    def nextColor(): 
     global score 
     global timeLeft 

     if timeLeft > 0: 
      e.focus_set() 
      if e.get().lower() == colors[1].lower(): 
       score += 1 

     e.delete(0, Tkinter.END) 
     random.shuffle(colors) 
     label.config(fg=str(colors[1]), text=str(colors[0])) 
     scoreLabel.config(text="Score: " + str(score)) 

    def countdown(): 
     global timeLeft 

     if timeLeft > 0: 
      timeLeft -= 1 
      time.config(text="Time Left: " + str(timeLeft)) 
      time.after(1000, countdown) 

これは私がインデントしてプログラムを見た後

C:\Python27\python.exe D:/pythonaldecoa/TypingGameColor.py 
Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__ 
    return self.func(*args) 
    File "D:/pythonaldecoa/TypingGameColor.py", line 17, in startGame 
    if timeLeft == 30: 
NameError: global name 'timeLeft' is not defined 

を得た誤りである、物事はより明確である: 「timeleftに」と 'score'はグローバルではなく、関数 'Game()'のローカルです。 ネストした関数nextColor()およびcountdown()でそれらを使用するには、グローバルではなく非ローカルとして宣言する必要があります。

+2

あなたの質問は何ですか? – zipa

+0

'Game'はクラスではなく関数ですか? –

答えて

2

あなたの質問は非常にはっきりしませんでしたが、間違ったキーワードを使用しているようです。はい、これらの変数はローカルではありませんが、globalキーワードを使用して定義しましたが、nonlocalキーワードを使用すると、pythonはグローバルスコープ内のこれらの変数を表示しないため、globalを使用することはできません。例えばnonlocal rules documented here

、この変更:このソリューションは、2.7では動作しません

def Game(): 
    ... 
    score ... 
    timeleft ... 
    ... 
    def nextColor(): 
     nonlocal score 
     nonlocal timeLeft 

     if timeLeft > 0: 
      e.focus_set() 
      if e.get().lower() == colors[1].lower(): 
       score += 1 

:これまで

def Game(): 
    ... 
    score ... 
    timeleft ... 
    ... 
    def nextColor(): 
     global score 
     global timeLeft 

     if timeLeft > 0: 
      e.focus_set() 
      if e.get().lower() == colors[1].lower(): 
       score += 1 

を。あなたが持っていない場合は、2.7を使用してはならないが、これは割り当てのためか、レガシーコードベースのためであれば、あなたはソリューションdiscussed here経由2.7の制限を回避することができます

def outer(): 
    d = {'y' : 0} 
    def inner(): 
     d['y'] += 1 
     return d['y'] 
    return inner 

f = outer() 
print(f(), f(), f()) #prints 1 2 3 

この作品ローカルではない変数を読むことができるので、変数を変更することはできないので、ある種の変更可能なコンテナが必要です。あなたの状況では

、これは次のようになります。

def Game(): 
    ... 
    game_stats['score'] ... 
    game_stats['timeLeft'] ... 
    ... 
    def nextColor(): 

     if game_stats['timeLeft'] > 0: 
      e.focus_set() 
      if e.get().lower() == colors[1].lower(): 
       game_stats['score'] += 1 
+0

私はnonlocalが良い解決策であることに同意しますが、OPが3.Xを使用している場合にのみ、それは2.7には存在しません。残念ながら、OPの呼び出しスタックには "C:\ Python27"が含まれているので、私は彼が3.Xのみの機能を使用することはできないと推測しています。 – Kevin

+0

@Kevin 2.7タグはありません... – snb

+0

質問タイトルで "nonlocalを使用できません"というのは、2つの解釈があります。彼は2.7を使用しているため使用できません。彼はインストラクターがこの運動のためにそれを使用することを禁じたので、彼はそれを使用することはできません。どちらの場合でも、非ローカルを使用しないソリューションを探すこともできます。 – Kevin

関連する問題