簡體   English   中英

python - 如何在python中將關鍵字調用到字符串中的字典?

[英]How can I call an keyword to an dictionary inside of a string in python?

我正在嘗試在我編寫的終端游戲中從字典中保存一些數據,我在字典中獲得了分數,我想調用這些分數以將它們寫在筆記上。 我試過了:

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

但這似乎不起作用。 如何在不重寫如何保存分數的情況下解決這個問題?

問題可能只是您在使用 f 字符串時需要使用不同的" / ' ,否則他們會認為他們已經提前結束了。

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'])}")

注意: f-string 表達式部分不能包含反斜杠

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM