1

I am trying to save some data from a dictionary in a terminal game I wrote, I've got the scores inside of a dictionary and I want to call those scores to write them on a note. I tried:

with open(path, 'w') as File:
File.write(f"player score is {str(score[\"Player\"])}\n"
           f"computer score is {str(score[\"computer\"])}")

but this does not seem to work. How can I solve this problem without rewriting how I save scores?

Willem
  • 81
  • 9
  • Do you want to append it to the end of the file? See https://stackoverflow.com/questions/1466000/difference-between-modes-a-a-w-w-and-r-in-built-in-open-function. – The Amateur Coder Jun 14 '22 at 16:51

1 Answers1

1

The issue may just be that you need to use differing " / ' when working with f-strings, or they'll think they've ended early.

with open(path, 'w') as File:
    File.write(f"player score is {str(score['Player'])}\n")
    File.write(f"computer score is {str(score['computer'])}")

Note: f-string expression part cannot include a backslash

BeRT2me
  • 12,699
  • 2
  • 13
  • 31