私はPythonを使ってInform 7(inform7.com)の基本機能をエミュレートしようとしています。室内にすべてのオブジェクトを印刷するように部屋を設定しようとしていて、部屋に何もオブジェクトがない場合は、まだIn this room there is a
が印刷されます。コードの関連部分はここにあります。空リストのPythonバグ
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
という行には決して届かない。私はしばらくそれを修正しようとしてきましたが、それは動作していません。また、部屋に2つのアイテムがあると、最初のものが表示されません。多分私は別の質問にそれを置くべきです。事前にお返事ありがとう!
ループからそのステートメントを移動します。リストが空の場合、ループはまったく実行されません。第二に、なぜあなたは文字列に単に文字列を追加するのではなく、stringtoappendを使用しています – Natecat
私はそれを試しましたが、うまくいきませんでした。 – Ranger
ああ、私はそれを誤って削除* facepalm * – Ranger