Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python上制作前5名记分板?_Python - Fatal编程技术网

如何在python上制作前5名记分板?

如何在python上制作前5名记分板?,python,Python,我一直在尝试从csv文件制作一个前5名的记分板,但我正在努力 这是我的代码,如果有人能帮忙,我将不胜感激 while True: if attempt == 2: print("You have no more guesses...You Loose!") print("Here is the scoreboard") user = str(input("Enter a name to save your highscore: ")) file = open (

我一直在尝试从csv文件制作一个前5名的记分板,但我正在努力

这是我的代码,如果有人能帮忙,我将不胜感激

while True:
if attempt == 2:
    print("You have no more guesses...You Loose!")
    print("Here is the scoreboard")

    user = str(input("Enter a name to save your highscore: "))
    file = open ("scoreboard.csv", "a")
    file.write("\n")
    file.write(user)
    file.write(",")
    file.write(str(points))
    file.write("pts")
    file.write("\n")
你可以试着用这个。 你不需要在起点和终点换行,除非你想在它们之间换行。 您还希望使用“带打开”自动关闭文件

while True:
if attempt == 2:
    print("You have no more guesses...You Loose!")
    print("Here is the scoreboard")
    user = str(input("Enter a name to save your highscore: "))
    with open("scoreboard.csv", "a") as output:
        output.write("\n"+user+","+str(points)+" pts")


CSV模块非常方便。
用户
将跟随您的输入。
它用于Python 3.6+

导入csv
打开('scoreboard.csv','a')作为f:
writer=csv.writer(f)
writer.writerow([user,f{points}pts]]
scoreboard.csv
示例:user=“john”,分值=10

john,10pts
将csv设置为:

david,12
bill,8
joe,18
henry,15
nathan,10
您可以使用此功能:

导入csv
def top_five():
top=[]
所有_分数=[]
打开('scores.csv',换行='')作为csvfile:
分数=csv.reader(csvfile)
对于分数中的行:
所有的分数。追加((第[0]行,第[1]行)
顶部=已排序(
所有的分数,
key=lambda分数:分数[1],
反向=真)
返回顶部[:5]
打印(前五名()
如果您将“pts”保存在csv中,请在所有分数列表中插入之前解析结果。 如果您有列名,请跳过for循环中的第一行

来源

您询问的具体问题是什么?