samedi 28 janvier 2017

Python bug with empty list

I'm using python to try to emulate the basic functions of Inform 7 (inform7.com). When I am trying to set up the rooms printing out all the objects in them, and there are no objects in the room, it still prints out In this room there is a. The relevant part of the code is here.

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

. The line if not self.objects_in_room: fullstring = '' break is never being reached, even when I have verified that the list is empty by removing all the items. I've been trying to fix it for a while, but it hasn't been working. Also annoying, when I have two items in a room, it isn't displaying the first. Maybe I should put that in another question. Thanks in advance for an answer!

Aucun commentaire:

Enregistrer un commentaire