What is the correct syntax for this operation?

Viewed 141

Writing a game and no matter what I try it keeps giving me a syntax error for the 'as'.

I have tried looking through StackOverflow / changing my code to find a workaround but I have been unable to fix it so far

winner = input("Winner test username")
winnerscore = input("Test score")
open("leaderboardtest.txt","a") as leaderboard
leaderboard.write = "/n" , winner , " : " , winnerscore

(winner and winnerscore variables would have been made earlier just wrote it here during testing)

Invalid syntax highlight on the 'as'.

(I know this is a comparatively simple problem to other things on StackOverflow but I would appreciate any support.)

2 Answers

Looks like you have used incorrect syntax for writing text into file.

f = open("leaderboardtest.txt","a") # open file for append operation
f.write('\n' + winner + ' : ' + winnerscore) # write data whatever you want
f.close() # close file

There you go.

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)
Related