我已经编程了一个游戏,在这个游戏中,你可以得到玩家的当前分数(分数),然后有一个保存在.txt文档中的最高得分(高分),我需要能够比较这些值,如果分数>高分,我需要能够更新它,我不知道如何更新,任何帮助都将不胜感激。在
file = open('save.txt','r+')
saved = file.read()
file.close()
high_score = saved
high_score = int(high_score)
global score
score = 21
def checkscore():
if score > high_score:
file = open('save.txt' , 'w+')
file.write(file.read().replace(saved,str(score)))
file.close()
else:
file.close()
return
checkscore()
这是我迄今为止所做的,只是删除了文档中的内容。在
你只需要一个函数来替换分数。比如:
编辑:它使用context manager(带。。。作为。您也可以read here。在
相关问题 更多 >
编程相关推荐