【发布时间】:2018-11-05 22:39:18
【问题描述】:
我将此代码作为更大的骰子游戏程序的一部分,以将高分保存在单独的文件中(以及正常的获胜分数文件),但它总是保存高分,无论它是更高还是更低,也不会' t 将其保存在 Winning_scores 文件中 它还以 ('Name: ', 'John', 'Score: ', '10', '\n') 的形式保存它,而不是单独保存变量,因为 str 因为否则我得到 'write() argument must be str, not tuple' 我也不太确定如何解决
tot1 = 5
tot2 = 1
name1 = ('John')
while True: #Code to find & print the winner
if tot1 > tot2:
print("Player 1 is the winner!")
#Opens file & appends the winning score at the end of it
tot1 = str(tot1)#Turns the score into a str
win_score = open("Winning_scores.txt", "a")
winner = ("Name: "+name1+" Score: "+tot1)
win_score.write(winner)
win_score.write("\n")
win_score.close()
print("Score saved.")
hisc = open("Winning_scores.txt", "w+")
highscore = hisc.read()
highscore_in_no = (highscore)
highscore_in_no = highscore_in_no
if tot1 > highscore_in_no:
print("Exceeded high score.")
highscore_in_no = tot1
hiscore = open("High_scores.txt", "a")
winner = ("Name: ",name1,"Score: ",tot1,"\n")
hiscore.write(winner)
hiscore.close()
print("High score saved.")
break
【问题讨论】:
标签: python file-handling