我是一个新手,正在努力为我的漫画书创建一个基本的“数据库”。 但是,我有一个问题。
我们的想法是把每个新条目列为一个列表。 我想我可以把这些列表设置成如下所示。
[Thor, 50, Marvel]
[Thor, 51, Marvel]
[Thor, 52, Marvel]
...
eventually, I 'd like to include entries for story arc, writer, artist, etc.
但是,我使用下面的代码进入漫画,发现每个新条目都被添加到列表的末尾。
option = 0
comicdb = []
while option != 3:
print "--------------------------"
print "1. Add a New Comic Book"
print "2. Print the Database"
print "3. Quit"
option = int(raw_input("Pick an Option: "))
if option == 1:
title = raw_input("Comic Book Title: ")
issue = int(raw_input("Issue Number: "))
publisher = raw_input("Publisher: ")
comicdb.append(title)
comicdb.append(issue)
comicdb.append(publisher)
print comicdb
运行代码几次之后,列表如下:
['Thor', 50, 'Marvel', 'Thor', 51, 'Marvel', 'Thor', 52, 'Marvel']
我想下列事情之一是错误的,但我想不通:
救命啊!
答案很简单。在列表中插入3个单词,而不是附加包含3个单词的列表。
应该是这样的:
你需要一个列表或字典列表。
您应该使用嵌套结构。
相关问题 更多 >
编程相关推荐