我正在使用 python 来模拟 Inform 7 ( inform7.com ) 的基本功能。当我尝试设置房间时打印出其中的所有对象,并且房间中没有对象,它仍然会打印出来In this room there is a
。代码的相关部分在这里。
def __repr__(self):
if self.room_firstrun:
for i in things:
if things[i] == self.name:
self.objects_in_room.append(i)
self.room_firstrun = False
fullstring = 'In this room, there is a '
for i in self.objects_in_room:
if not self.objects_in_room:
fullstring = ''
break
elif i == self.objects_in_room[len(self.objects_in_room) - 1] and len(self.objects_in_room) > 2:
stringtoappend = ', and a ' + i + '.'
elif i == self.objects_in_room[len(self.objects_in_room) - 1] and len(self.objects_in_room) == 2:
stringtoappend = ' and a ' + i + '.'
elif i == self.objects_in_room[len(self.objects_in_room) - 1] and len(self.objects_in_room) == 1:
stringtoappend = i + '.'
elif i == self.objects_in_room[0]:
stringtoappend = i
else:
stringtoappend = ', a ' + i
fullstring += stringtoappend
stringtoappend = ''
return '\n----------' + self.name + '----------\n\n' + self.desc + '\n\n' + fullstring
. if not self.objects_in_room: fullstring = '' break
即使我通过删除所有项目来验证列表为空,也永远不会到达该行。我一直在尝试修复它一段时间,但它一直没有工作。也很烦人,当我在一个房间里有两件物品时,它不会显示第一个。也许我应该把它放在另一个问题中。提前感谢您的回答!