if not os.path.exists(txt_file):
with open(txt_file, 'w') as f:
f.write('Score: {score}\n'
+ 'Name: {name}')
with open(txt_file, 'r') as f:
previous_score = f.readline()
# compare the previous score with current score and write the highest score
if int(previous_score) < int(score):
with open(txt_file, 'w') as f:
f.write('Score: {score}\n'
+ 'Name: {name}')
我知道,因为我在线上有Score:
,所以它不只是分数而返回。如果使用re.findall
,我只能得到分数,但它会变成一个列表,但我仍然不能使用int(previous_score) < int(score)
。我该如何进行这项工作?
谢谢上一位评论员Sachin,他帮助我做到了这一点。
怎么了previous_score = int(f.readline().split()[-1])