Python 空列表错误

Python bug with empty list

我正在使用 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,即使我已通过删除所有项目验证列表为空。我已经尝试修复它一段时间了,但一直没有用。同样烦人的是,当我在一个房间里有两件物品时,它没有显示第一件。也许我应该把它放在另一个问题中。预先感谢您的回答!

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

或者您可以这样做:如果您使用 python 3 使用 for..else 语句。

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?为什么不直接将它添加到完整字符串中?