错误
addGuest中的文件“D:/hotel2.py”,第11行 客人[I] .append(名称) IndexError:列表索引超出范围
hotel2.py
def addGuest():
i = 0
j = 0
global guestid
name = input("Please enter guest name:\n")
guests[i].append(name)
guests[j].append(guestid)
print("Guest %s has been created with guest ID: %d" % (guests[i], guests[j]))
i += 1
j += 1
guestid += 1
我真正想要做的是存储guestid
和guestname
,以便在我输入guestname
时显示guestid
。但是这个功能不允许我这样做。
答案 0 :(得分:2)
最好使用key:value
对的字典而不是两个列表:
guestid2name = {}
def addGuest():
name = raw_input('enter guest name:\n')
global guestid
guestid2name[guestid] = name
print("Guest %s has been created with guest ID: %d" % (name, guestid))
这将创建guestid的guestid2name
数据结构,其中guest虚拟机的名称为value。它比使用两个不同的列表来实现同样的效果要高效得多。