Python 如何将字符串和整数添加到文件中,然后对其进行排序?

Python 如何将字符串和整数添加到文件中,然后对其进行排序?,python,python-3.x,Python,Python 3.x,在我的代码中,程序要求输入用户名和分数。不过,我可以将分数发送到一个文件并对其进行排序。有没有办法更改代码,以便将播放器名称发送到文件中,并与相应的编号一起排序 player_1=input('What is your name?') points1=input('What is your score?') player_2=input('What is your name?') points2=input('What is your score?') leaderboard=input('T

在我的代码中,程序要求输入用户名和分数。不过,我可以将分数发送到一个文件并对其进行排序。有没有办法更改代码,以便将播放器名称发送到文件中,并与相应的编号一起排序

player_1=input('What is your name?')
points1=input('What is your score?')
player_2=input('What is your name?')
points2=input('What is your score?')

leaderboard=input('Type YES if you would like to see the tp scores and NO if you wouldnt')
while leaderboard=='YES':
    Scorefile=open('scores.txt','a')
    Scorefile.write(points1)
    Scorefile.write("\n")
    Scorefile.write(points2)
    Scorefile.write("\n")
    Scorefile.close()
    Scorefile = open('scores.txt','r')
    with open('scores.txt','r') as Scorefile:
        scores=Scorefile.readline()
        List1=[]
        while scores:
            scores2=(scores.strip())
            List1.append(scores2)
            scores=Scorefile.readline()
    for i in range(0, len(List1)):
        List1[i] = int(List1[i])        
    List1.sort(reverse=True)

    print(List1[0:5])
    leaderboard='ANYTHING'
while leaderboard=='NO':
    sys.exit()

为什么不创建两个文件,并具有第一个文件存储字符串和第二个文件存储整数。
然后,如果需要,从两者中提取信息。非常简单。

是的,有一种方法可以做到。你需要什么帮助?请阅读。这段代码也需要重写。例如,您知道如何使用
with
语句,那么为什么要手动打开和关闭
scores.txt
?为什么在
时使用
而不是
如果
?为什么要执行
sys.exit()
,而不是让它自己退出?为什么只将5个分数写回文件?另外,使用
print(points1,points2,sep='\n',file=Scorefile)
如果我这样做了,我将如何对字符串进行排序?我对您所指的字符串排序非常困惑。像字母顺序?我需要更多的信息来帮助我理解“排序”的含义。