所以问题是当我运行排行榜代码时,它正确地输出了一些排行榜,但是添加了一行不正确的代码。你知道吗
我试过删除txt文件的内容,但没有成功。你知道吗
def save():
global totalone,totaltwo,name1,name2,rounds
file=open("scores.txt","a")
if totalone < totaltwo:
#name2 V name1 | totaltwo
file.write(f"Scored {totaltwo} | Winner:{name2} V {name1} | played 0 H2H rounds\r")
elif totaltwo < totalone:
#name1 V name2 | totalone
file.write(f"Scored {totalone} | Winner:{name1} V {name2} | Played 0 H2H rounds\r")
else:
#name1 V name2 | Tied | rounds
if totalone < totaltwo:
file.write(f"Scored {totalone} | Winner:{name1} V {name2} | Tied | Played {rounds} H2H rounds\r")
elif totaltwo < totalone:
file.write(f"Scored {totaltwo} | Winner:{name2} V {name1} | Tied | Played {rounds} H2H rounds\r")
file.close()
def leaderboard():
file=open("scores.txt","r")
data=file.readlines()
data.sort(reverse=True)
x = 0
for i in range(len(data)):
print((data[i].strip()))
x += 1
if x == 5:
break
file.close()
结果显示了一个无序的列表。你知道吗
得分20分获奖者:约翰五世肯尼迪打了0个两小时的回合
得分40分获奖者:约翰五世肯尼迪打了0个两小时的回合
得分10分获奖者:约翰五世肯尼迪打了0个两小时的回合
得分80 |获奖者:约翰五世肯尼迪打了0个H2H回合
请参阅@mkrieger1的解决方案以获得真正正确的解决方案(实际上是在格式化之前根据具体值进行排序)。你知道吗
对于一种快速而肮脏的解决方案,只需将
data.sort(...)
替换为以下内容:它的作用是告诉排序使用第一个和第二个空格之间的值(恰好是分数),并将其转换为int(因此排序是数字的,而不是字母顺序的)
澄清:
这是一个非常糟糕的做法,因为如果您更改行格式,它可能会中断(这与引线板的排序方式无关,只是与显示有关)。你知道吗
所以,就软件工程而言,这不是一个好的实践。动作快,效果好。你知道吗
这是按字母顺序排列的,所以
是“正确的”。你知道吗
在格式化输出行之前,需要根据数值分数(变量
totalone
或totaltwo
进行排序。你知道吗相关问题 更多 >
编程相关推荐