-3

我正在使用 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即使我通过删除所有项目来验证列表为空,也永远不会到达该行。我一直在尝试修复它一段时间,但它一直没有工作。也很烦人,当我在一个房间里有两件物品时,它不会显示第一个。也许我应该把它放在另一个问题中。提前感谢您的回答!

4

1 回答 1

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

或者你可以这样做:如果你使用 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?为什么不直接将其添加到完整字符串中?

于 2017-01-28T21:07:19.793 回答