文件创建写入文件

时间:2019-03-30 15:33:11

标签: python

我很难在游戏的文本文件中写入一些分数...

if total_player1>total_player2:           
    f=open("mydatafilescores.txt","w")

    f.write("The winner of the match was")
    f.write(str(auth_user1))
    f.write("with an outstanding score of")
    f.write(str(total_player1))
    f.close            
print("\nGood Game. Game is over!")
elif total_player2>total_player1:

f=open("mydatafilescores.txt","w")

f.write("The winner of the match was")
f.write(str(auth_user2))
f.write("with an outstanding score of")
f.write(str(total_player2))
f.close            
print("\nGood Game. Game is over!")   

变量已经被计算出来了,问题是实际文本文档中出现了nothing。实际的缩进在我的程序中是正确的,但是iv有点奇怪地将其复制到了stackoverflow网站上。任何帮助,将不胜感激!谢谢

1 个答案:

答案 0 :(得分:0)

更改

f.close

f.close()

或使用

with open('file.txt', 'w') as f:
    # your code