好吧,所以我希望当用户想要检查高分时,输出会按降序打印数据请记住,都是.txt文件中的名称和数字,这就是为什么我觉得这很难。如果你还需要什么,请告诉我,在我将如何阅读.txt文件,然后按降序排列数据
def highscore():
global line #sets global variable
for line in open('score.txt'):
print(line)
#=================================================================================
def new_highscores():
global name, f #sets global variables
if score >= 1:#if score is equal or more than 1 run code below
name = input('what is your name? ')
f = open ('score.txt', 'a') #opens score.txt file and put it into append mode
f.write (str(name)) #write name on .txt file
f.write (' - ') #write - on .txt file
f.write (str(score)) #write score on .txt file
f.write ('\n') #signifies end of line
f.close() #closes .txtfile
if score <= 0: #if score is equal to zero go back to menu 2
menu2()
我加入这个以防万一有在我在文件
你如何运行它,你从哪里调用这些函数? – davedwards
是的,我可以添加完整的代码,如果你想 – Jesuspepper
@Jesuspepper你看看我的答案?如果您对此有任何疑问或者无法使用,请添加评论,但我认为它应该满足您的需求。 –