本文主要是介绍利用字典和random库做一个背诵古诗词的小程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import randomPHRASES ={"君恩如水向东流,得宠忧移失宠愁。":"莫向尊前奏花落,凉风只在殿西头。","锦里烟尘外,江村八九家。":"圆荷浮小叶,细麦落轻花。","应是天仙狂醉,":"乱把白云揉碎。","安得五彩虹,":"驾天作长桥。","对酒当歌,人生几何!":"譬如朝露,去日苦多。","山不厌高,海不厌深。":"周公吐哺,天下归心。" }# keep going until they hit CTRL-D
try:while True:snippets = list(PHRASES.keys())random.shuffle(snippets) # random.shuffle()是打乱列表的顺序。for snippet in snippets:phrase = PHRASES[snippet]question, answer =snippet,phrase # convert()就是上面定义的函数print(question)input(">")print(f"ANSWER: {answer}\n\n")
except EOFError:print("\nBye")
运行结果如下:
这篇关于利用字典和random库做一个背诵古诗词的小程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!