高分代码无法正确保存分数
High score code not saving scores properly
我将这段代码作为一个更大的骰子游戏程序的一部分,将最高分保存在一个单独的文件中(以及正常的获胜分数文件),但它总是保存最高分,无论它是高分还是低分,而且也不将其保存在 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
这是你的问题:
winner = ("Name: "+name1+" Score: "+tot1)
win_score.write(winner)
当您在 Python 中用括号括起一个值时,您是在说它是一个 元组 ,有点类似于列表。这是一个希望更清楚的例子
one = "foo"
two = "bar"
this_is_a_tuple = (one, two)
this_is_also_a_tuple = (one)
this_is_not_a_tuple = one + two
this_is_a_tuple = (one + two)
您的 winner 变量是元组,而不是字符串。
为了使用hiscore.write(winner)
,获胜者应该是如下字符串:
winner = "Name: " + name1 + "Score: " + tot1 + "\n"
或更好的可读性:
winner = "Name: {name1} Score: {tot1}\n".format(**locals())
您还可以将现有的获胜者元组加入字符串:
hiscore.write(' '.join(winner))
我将这段代码作为一个更大的骰子游戏程序的一部分,将最高分保存在一个单独的文件中(以及正常的获胜分数文件),但它总是保存最高分,无论它是高分还是低分,而且也不将其保存在 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
这是你的问题:
winner = ("Name: "+name1+" Score: "+tot1)
win_score.write(winner)
当您在 Python 中用括号括起一个值时,您是在说它是一个 元组 ,有点类似于列表。这是一个希望更清楚的例子
one = "foo"
two = "bar"
this_is_a_tuple = (one, two)
this_is_also_a_tuple = (one)
this_is_not_a_tuple = one + two
this_is_a_tuple = (one + two)
您的 winner 变量是元组,而不是字符串。
为了使用hiscore.write(winner)
,获胜者应该是如下字符串:
winner = "Name: " + name1 + "Score: " + tot1 + "\n"
或更好的可读性:
winner = "Name: {name1} Score: {tot1}\n".format(**locals())
您还可以将现有的获胜者元组加入字符串:
hiscore.write(' '.join(winner))