繁体   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