编写游戏,无论我尝试什么,都会不断给我“ as”的语法错误。
我尝试浏览StackOverflow /更改代码以找到解决方法,但到目前为止我仍无法修复
winner = input("Winner test username")
winnerscore = input("Test score")
open("leaderboardtest.txt","a") as leaderboard
leaderboard.write = "/n" , winner , " : " , winnerscore
(获胜者和获胜者的变量早些时候就已经制作出来了,只是在测试过程中在这里写下)
无效的语法高亮显示在“ as”上。
(相对于StackOverflow上的其他事情,我知道这是一个相对简单的问题,但我会提供任何支持。)
答案 0 :(得分:6)
看起来您使用了错误的语法将文本写入文件。
f = open("leaderboardtest.txt","a") # open file for append operation
f.write('\n' + winner + ' : ' + winnerscore) # write data whatever you want
f.close() # close file
答案 1 :(得分:5)
你去了。
winner = input("Winner test username")
winnerscore = input("Test score")
with open("leaderboardtest.txt","a") as leaderboard:
string_to_write = "\n" + winner + " : " + winnerscore
leaderboard.write(string_to_write)