打印时如何添加行号
这是我在道森书中为挑战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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论