guests = ['yara', 'fatma', 'hamdi', 'monica', 'mirna']
cannot_make_it = ['hamdi', 'monica', 'mirna']
guests.pop()
guests.pop()
guests.pop()
guests.append('mona')
guests.append('nour')
guests.insert(0, 'salma')
guests.insert(3, 'koko')
guests.append('soso')
guests.pop(2)
guests.pop(3)
guests.pop(4)
guests.pop(5)
guests.pop(6)
print('Only the following can come due to shortage in places:')
print(guests[0])
print(guests[1])
print('Sorry, ' + guests.pop(2) + ' you cannot come.')
print('Sorry, ' + guests.pop(3) + ' you cannot come.')
print('Sorry, ' + guests.pop(4) + ' you cannot come.')
print('Sorry, ' + guests.pop(5) + ' you cannot come.')
print('Sorry, ' + guests.pop(6) + ' you cannot come.')
错误消息是:"D:\pythonProject\3-7收缩来宾list\venv\Scripts\python.exe“"D:/pythonProject/3-7收缩来宾列表/收缩来宾list.py”回溯(最近一次调用):文件"D:/pythonProject/3-7收缩来宾列表/收缩来宾list.py",第19行,在guests.pop(5) IndexError: pop索引超出范围
发布于 2021-07-05 18:20:54
这是因为当您执行pop(5)时,该索引中没有任何元素,因为访客列表从前3个pop中一直在缩小。
https://stackoverflow.com/questions/68249366
复制相似问题