打印时如何添加行号

发布于 2025-01-17 10:01:40 字数 920 浏览 2 评论 0原文

这是我在道森书中为挑战7.2创建的功能,但想对其进行一些修改。不垃圾邮件,我只是发送关键位置,因为其他地方与问题无关。

def results(score):  
     name = input("What's your name? ")
     s = shelve.open("results.dat")
     s[str(score)] = [name]
     s.sync()
     scores = list(s.keys())
     scores.sort(reverse=True)
     print("Top 5 results in quiz game:\n")
     for i in scores[:5]:
         print("Points:", i, "Player:", "".join(s[i]))
     s.close()

我的输出外观:

Points: 8 player: Chris
Points: 6 player: klsjahd
Points: 5 player: dlahdlksjah
Points: 4 player: dnlsandlknasd
Points: 3 player: bleble

我想添加一个行号,因此我可以显示明显的内容,例如哪个顶部。

TOP 1: Points: 8 player: Chris
TOP 2: Points: 6 player: klsjahd
TOP 3: Points: 5 player: dlahdlksjah
TOP 4: Points: 4 player: dnlsandlknasd
TOP 5: Points: 3 player: bleble

Here's a function I created for challenge 7.2 in Dawson book, but wanted to modify it a little bit. To not spam I'm sending just key place as other is not related to the problem.

def results(score):  
     name = input("What's your name? ")
     s = shelve.open("results.dat")
     s[str(score)] = [name]
     s.sync()
     scores = list(s.keys())
     scores.sort(reverse=True)
     print("Top 5 results in quiz game:\n")
     for i in scores[:5]:
         print("Points:", i, "Player:", "".join(s[i]))
     s.close()

My output looks:

Points: 8 player: Chris
Points: 6 player: klsjahd
Points: 5 player: dlahdlksjah
Points: 4 player: dnlsandlknasd
Points: 3 player: bleble

I'd want to add a row number, so I can show obvious thing like which top is it.

TOP 1: Points: 8 player: Chris
TOP 2: Points: 6 player: klsjahd
TOP 3: Points: 5 player: dlahdlksjah
TOP 4: Points: 4 player: dnlsandlknasd
TOP 5: Points: 3 player: bleble

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文