在.txt文件中,我保存了“('k',1.67)”,想分别访问名称和分数,但是当我尝试访问元组值时,它说它有太多值需要解压。
我该如何解决?
**ValueError: too many values to unpack (expected 2)**
def overwrite(highest_score_details):
with open("score.txt", 'w') as save:
print(highest_score_details, file=save)
def get_score():
with open("score.txt", 'r') as load:
contents = load.readline()
name_read, score_read = contents
return contents
name = input("Enter your name: ")
start_time = my_timer()
for i in range(total_questions):
make_question()
end_time = my_timer()
total_score = time_taken + wrong_answer_penalty
get_score()
highest_score_details = (name, total_score)
overwrite(highest_score_details)
[抱歉,我是编程新手,需要帮助。我做了一个简单的数学测试,我希望能够保存分数,检查以前的最高分数,如果更高,则覆盖最高分数。 ...
readline
返回一个字符串,它是字符的集合。为了正确地将此字符串转换为可以按您需要的方式使用的数据,您必须定义一种解析它的方法。