剛剛在寫代碼的時候遇見了一個問題
代碼如下
class Engine(object):
def __init__(self, scene_map):
self.scene_map = scene_map
def play(self):
current_scene = self.scene_map.opening_scene() #當前場景
last_scene = self.scene_map.next_scene('lanmo')
while current_scene != last_scene:
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
class Base():
def enter(self):
print('ok')
class Map(object):
modules = {
'base' : Base(),
}
def __init__(self, start_scene):
self.start_scene = start_scene
def next_scene(self, scene_neme):
val = Map.modules.get(scene_name)
return val
def opening_scene(self):
return self.next_scene(self.start_scene)
a_map = Map('base')
a_game = Engine(a_map)
a_game.play()
報錯:
PS E:\ic> python try.py
Traceback (most recent call last):
File "E:\ic\try.py", line 36, in <module>
a_game.play()
File "E:\ic\try.py", line 7, in play
current_scene = self.scene_map.opening_scene() #當前場景
File "E:\ic\try.py", line 32, in opening_scene
return self.next_scene(self.start_scene)
File "E:\ic\try.py", line 28, in next_scene
val = Map.modules.get(scene_name)
NameError: name 'scene_name' is not defined
哪位大佬能幫忙解決一下,不勝感激!!
uj5u.com熱心網友回復:
形參名打錯了,neme
uj5u.com熱心網友回復:
def next_scene(self, scene_neme): #這里scene_neme 是不是應該改成scene_name,是不是打錯了val = Map.modules.get(scene_name)
return val
uj5u.com熱心網友回復:
就是引數name寫成neme了,改回來就好了uj5u.com熱心網友回復:
呵呵呵,這不是解決問題,是在玩“找碴”游戲。如果用有代碼檢查IDE應該在寫完后就能標注出來問題所在,用人眼去找真的很累的。
vs code 應該能解決這種普通問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/234464.html
