2013-07-20 148 views
0

我對編程非常陌生(所以很抱歉,如果我沒有提出這個問題的話)。 這是LPTHW練習36:NameError:全局名稱'stealth'未定義

我的錯誤:

Traceback (most recent call last): 
    File "ex36.py", line 329, in <module> 
    start() 
    File "ex36.py", line 149, in start 
    arena() 
    File "ex36.py", line 161, in arena 
    if stealth == True: 
NameError: global name 'stealth' is not defined 

我的假設: 我想「隱身」在前面的功能定義,啓動(),但該定義沒有進行過到競技場()。我該如何解決這個問題,爲什麼從1個功能'隱身'到另一個功能呢?我的代碼

(正在進行基於文本的遊戲):

from sys import argv 

    script, enemy = argv 
    ... 
    def start(): 
     print """ Choose a skill to train in 
     """ 
     stealth = False 
     gun = False 
     knife = False 
     heal = False 
     skill = raw_input("> ") 

     if 'gun' in skill: 
      print """ 
      """ 
      gun = True 
      skill = gun 
     ... 
     else: 
      dead() 

     arena() 

    def arena(): 
     print """ You enter the arena. Will you: 
     hide, hunt for food, or search for water? 
      """ 

     path = raw_input("> ") 

     if "hide" in path: 
      print """ Hide 
      """ 

      if stealth == True: 
       print """ Witness 
       """ 
       witness() 
      else: 
       battle() 
     ... 
     else: 
      print """ Dead 
      """ 
      dead() 
start() 

所有的意見是極大的讚賞。感謝您的幫助。

+0

也許變量是如此隱蔽了Python不能看到它。 – BrenBarn

+0

@BrenBarn或者它的作用域剛好在'start'中,不能從'arena'訪問... –

回答

0

在一個函數中本地定義的變量具有本地範圍,並且不能在另一個不相關函數內自動訪問。您可能需要考慮在從start(例如,)撥打stealtharena時通過。 arena(stealth),然後stealth將被定義爲的arena一個參數,即

def arena(stealth): 
相關問題