我正在使用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
。我一直试图修复它一段时间,但它一直没有工作。同样烦人的是,当我在一个房间里有两件物品时,它没有显示第一件物品。也许我应该把它放在另一个问题上。在此先感谢您的回答!
答案 0 :(得分:0)
if not self.objects_in_room
因为它位于for循环中,并且for循环仅在self.objects_in_room
不为空时运行。要解决此问题,请将if not self.objects_in_room
置于循环之外
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 '
if not self.objects_in_room:
fullstring = ''
for i in self.objects_in_room:
if i == self.objects_in_room[len(self.objects_in_room) - 1] and len(self.objects_in_room) > 2:
fullstring += ', and a {0}.'.format(i)
elif i == self.objects_in_room[len(self.objects_in_room) - 1] and len(self.objects_in_room) == 2:
fullstring += ' and a {0}.'.format(i)
elif i == self.objects_in_room[len(self.objects_in_room) - 1] and len(self.objects_in_room) == 1:
fullstring += '{0}.'.format(i)
elif i == self.objects_in_room[0]:
fullstring += i
else:
fullstring += ', a {0}'.format(i)
return '\n----------' + self.name + '----------\n\n' + self.desc + '\n\n' + fullstring
或者你可以这样做:如果你使用for..else
语句使用python 3。
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 i == self.objects_in_room[len(self.objects_in_room) - 1] and len(self.objects_in_room) > 2:
fullstring += ', and a {0}.'.format(i)
elif i == self.objects_in_room[len(self.objects_in_room) - 1] and len(self.objects_in_room) == 2:
fullstring += ' and a {0}.'.format(i)
elif i == self.objects_in_room[len(self.objects_in_room) - 1] and len(self.objects_in_room) == 1:
fullstring += '{0}.'.format(i)
elif i == self.objects_in_room[0]:
fullstring += i
else:
fullstring += ', a {0}'.format(i)
else:
fullstring = ''
return '\n----------' + self.name + '----------\n\n' + self.desc + '\n\n' + fullstring
你为什么使用stringtoappend?为什么不直接将它添加到fullstring?