Home > OS >  How can I call an keyword to an dictionary inside of a string in python?
How can I call an keyword to an dictionary inside of a string in python?

Time:06-15

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?

CodePudding user response:

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

  • Related