Python 2.7 无类型对象属性错误Python
在Zed Shaw的LPTHW之后,用python 2.7制作了一个文本游戏。它包括将不同的文件导入一个文件并调用它。游戏正在运行,但最后它给了我一个属性错误Python 2.7 无类型对象属性错误Python,python-2.7,Python 2.7,在Zed Shaw的LPTHW之后,用python 2.7制作了一个文本游戏。它包括将不同的文件导入一个文件并调用它。游戏正在运行,但最后它给了我一个属性错误 Traceback (most recent call last): File "Main.py", line 15, in <module> a_game.play() File "C:\mystuff\Escape\Game_Engine.py", line 17, in play next_scene_name = cu
Traceback (most recent call last):
File "Main.py", line 15, in <module> a_game.play()
File "C:\mystuff\Escape\Game_Engine.py", line 17, in play next_scene_name = current_scene.enter() #enter the current scene calling 'enter' function.
AttributeError: 'NoneType' object has no attribute 'enter'
类引擎(对象):
#以场景映射作为参数的构造函数
定义初始(自我,场景贴图):
self.scene\u map=scene\u map
#函数进入打开的场景
def播放(自我):
当前场景=self.scene\u map.opening\u scene()
尽管如此:
打印“>>>>>”
next_scene_name=当前_scene.enter()#输入当前场景调用“enter”函数。
current_scene=self.scene_map.next_scene(next_scene_name)#进入调用map函数的next scene
谢谢。没有叫“完工”的房间。当你到达终点时,它不会找到它。在主回路中容易固定 将播放更改为以下内容:
def play(self):
current_scene = self.scene_map.opening_scene()
while current_scene is not None:
print "<<<<<<<<<< MI-Escape from Rogue AI >>>>>>>>>>>>>> "
next_scene_name = current_scene.enter() #enter the current scene calling 'enter' function.
current_scene = self.scene_map.next_scene(next_scene_name) #to enter the next scene calling map function
def播放(自):
当前场景=self.scene\u map.opening\u scene()
当前_场景不是无时:
打印“>>>>>”
next_scene_name=当前_scene.enter()#输入当前场景调用“enter”函数。
current_scene=self.scene_map.next_scene(next_scene_name)#进入调用map函数的next scene
这意味着执行时未定义下一个场景名称
,该名称指的是当前场景
,该名称指的是自我场景映射。打开场景()。如果您在代码中后退一步或添加一些打印语句,您可能会发现该步骤失败。它可能是某个地方缺少的return
语句吗?忘记了变量赋值?是的,是“return finished”语句。纠正了。谢谢我跟着这一个工作。请问“is not None”是做什么的?这是一个检查,看看它是否是由于查找失败而导致的非类型。有很多基本的Python教程可以解释这一点,但在其他语言中类似于Null或NoN。你也可以写为“当当前场景!=None:”明白了。谢谢。
import Rooms
import Game_Engine
#create class map with a dictionary for scene reference.
class Map(object):
scenes = {
'entrance' : Rooms.Entrance(),
'control_room' : Rooms.Control_Room(),
'AI_vault' : Rooms.AI_Vault(),
'server_room' : Rooms.Server_Room(),
'roof' : Rooms.Roof(),
'death' : Rooms.Death()
}
#constructor with start_scene as argument.
def __init__(self,start_scene):
self.start_scene = start_scene
#function to retrieve scenes from dictionary
def next_scene(self,scene_name):
val = Map.scenes.get(scene_name)
return val
#using the next_scene function to display opening scene
def opening_scene(self):
return self.next_scene(self.start_scene)
class Engine(object):
#constructor with scene_map as argument
def __init__(self,scene_map):
self.scene_map = scene_map
#function to enter opening scene
def play(self):
current_scene = self.scene_map.opening_scene()
while True:
print "<<<<<<<<<< MI-Escape from Rogue AI >>>>>>>>>>>>>> "
next_scene_name = current_scene.enter() #enter the current scene calling 'enter' function.
current_scene = self.scene_map.next_scene(next_scene_name) #to enter the next scene calling map function
def play(self):
current_scene = self.scene_map.opening_scene()
while current_scene is not None:
print "<<<<<<<<<< MI-Escape from Rogue AI >>>>>>>>>>>>>> "
next_scene_name = current_scene.enter() #enter the current scene calling 'enter' function.
current_scene = self.scene_map.next_scene(next_scene_name) #to enter the next scene calling map function