【发布时间】:2020-05-12 18:56:35
【问题描述】:
使用 for 循环,我想打印列表中的每个项目。所以我做了这个代码:
from termcolor import colored # Ignore this and the "colored" function in the strings, just a
# package I installed.
titles = ["Dummy Story"]
bookshelf = {
"Dummy Story": ["Page 1", "Page 2", "Page 3", "Page 4"] # The list I want it to print
}
def readfun():
print(colored("So you want to read a story now?", "green"))
global titles
global bookshelf
print(titles)
storytoread = input(colored("Choose a book to read: ", "yellow"))
for page in storytoread:
print(page, end='')
readfun()
这是我正在制作的一种故事阅读项目。 storytoread 变量存储用户想要阅读的书名。所有信息均来自名为bookshelf 的字典。我希望它打印列表值的项目,因为storytoread 变量的值将被标识为字典bookshelf 中键值对的键。但是,它所做的只是打印存储在storytoread 中的字符串,而不是我希望它打印的字符串。我怎样才能做到这一点?
【问题讨论】:
-
你知道你的书在书架上。你去书架找书了吗?
-
嗯,你可以在标题中看到“虚拟故事”可用。所以我希望它是用户可以阅读的唯一一本书。
标签: python python-3.x dictionary