首页 > 解决方案 > 我如何获得打印声明来告诉我作者的死亡日期?

问题描述

我试图让它说“作者”在“日期”中死亡,但它不起作用。

authors = {
   "Charles Dickens": "1870",
   "William Thackeray": "1863",
   "Anthony Trollope": "1882",
   "Gerard Manley Hopkins": "1889"
   }
for author, date in authors.items():
   print( "%s" % authors + " died in "  + date)

标签: pythonpython-3.xloopsdictionary

解决方案


您只需将 print 中的变量名称从authors(您的完整字典)更改为author,代表集合的每个实例:

authors = {
   "Charles Dickens": "1870",
   "William Thackeray": "1863",
   "Anthony Trollope": "1882",
   "Gerard Manley Hopkins": "1889"
   }
for author, date in authors.items():
    print( "%s" % author + " died in "  + date)

这会产生以下输出:

Charles Dickens died in 1870
William Thackeray died in 1863
Anthony Trollope died in 1882
Gerard Manley Hopkins died in 1889

推荐阅读