【发布时间】:2021-12-27 04:03:02
【问题描述】:
我将尝试先在此处添加所有相关代码,然后再解释我的问题。更多的是 python 的新手,只是想做一个我觉得对我有挑战性的项目,所以我想做一些有点像“然后没有”、线索和我们中间的事情,如果那样的话有助于上下文。
功能 1 - 随机确定角色将移动到哪个房间,更新所有玩家房间信息,并将该角色添加到房间实例中的角色列表中。这似乎是问题所在?
#room_options is list of rooms that depends on what room char is currently in. Passed in from instance object.
all_rooms = [hallway, kitchen, living_room]
def set_next_room(room_options):
completed_chars = []
for room in all_rooms:
characters_in_room = room.get_occupants()
characters_in_room = characters_in_room[:]
#empties room so new characters can be assigned to room
room.characters.clear()
#if the person in characters in room is in completed characters, take them out of the characters in room list that will be run in next for loop
for char in characters_in_room[:]:
if char in completed_chars:
characters_in_room.remove(char)
#sets new rooms for each character in each room
for char in characters_in_room:
rand_int = randint(0, len(room_options)-1)
rand_room = room_options[rand_int]
#sets next room based on available choices
char.set_next_room(rand_room)
#adds character to the next room character list
next_room = function_mapping_rooms(char.get_next_room())
next_room.characters.append(char) #this line is not working??
#changes rooms from current to previous, next to current, and next to None
char.set_prev_room(char.get_current_room)
char.set_current_room(next_room.name)
char.set_next_room(None)
#adds person to the completed list so room determination is not duplicated
completed_chars.append(char)
函数 2 - 对问题不是特别重要,但包含在内是因为上面调用了此函数...更多上下文
def function_mapping_rooms(room_name):
characters = {
'hallway': hallway,
'kitchen': kitchen,
'living room': living_room,
'player death': player_death,
'accusation': accusation
}
return characters.get(room_name)
房间的基本示例类
class Kitchen:
def __init__(self, name, weapon, characters):
self.name = name
self.weapon = weapon
self.characters = characters
self.room_options = ['hallway', 'living room']
def enter(self):
set_next_room(self.room_options)
kitchen = Kitchen('kitchen', 'kitchen knife', [])
所以问题是这样的。根据我对代码的理解,第一个代码块中的next_room.characters.append(char) 行应该是在“厨房”类的实例中添加一个从characters_in_room 到self.characters 的字符。当我通过调试器运行代码时,房间的字符列表会更新。但是,一旦 set_next_room 函数完成并且我想使用新更新的字符列表,列表 self.characters 就会显示为空。有人对为什么会这样有任何建议吗?我猜它与记忆和存储有关?
提前致谢。
【问题讨论】:
-
你所描述的不应该发生。
next_room应该获得全局kitchen对象,next_room.characters.append应该在适当的位置修改列表。你可能会做assert next_room is kitchen,或者类似的东西。请注意,您可能不需要为每个房间设置不同的课程。房间只是Room类的不同实例,对吧? -
当你说“但是,一旦 set_next_room 函数完成并且我想使用新更新的字符列表,列表
self.characters出现为空” - 你的意思是self.characters是空的相同的过程对吗(因为代码尚未“完成”并且仍在运行)?您提出了“内存和存储”的潜在问题,所以我只是想确认您在进程结束后没有尝试访问self.characters,因为此时它将不再可访问,因为它不再在 RAM 中。 -
@Phoenix 很好的澄清问题。是的,我知道脚本仍在运行。我的意思是一旦
set_next_room函数在它被调用后完成。在,关于内存/存储:我想我想更多的是,我附加的next_room.characters列表是否存储在与kitchen对象的self.characters不同的位置的内存中?这就是为什么我在对象中得到一个空的self.characters列表? -
@TimRoberts 好的,所以我工作得更多了,我发现在循环
for room in all_rooms:内,每个房间的字符列表都会更新并包含名称。但是一旦我跳出那个循环,每个房间的字符列表都是空的。基本上我在循环内添加了一个打印语句,然后在它之外添加了一个打印语句并发现了差异。这是范围问题吗?需要以某种方式在循环外定义next_room? -
好的,仔细看,我发现您正在对这些
char对象调用多个方法,例如设置房间。这些方法是否有可能“帮助”从现有房间中删除自己?你做汤的厨师可能太多了。
标签: python class variables instance instantiation