【问题标题】:Python 3.8 issue - print of float with 5 digits after dot - error in PyCharm?Python 3.8 问题-在点后打印 5 位数字的浮点数-PyCharm 中的错误?
【发布时间】:2023-03-14 07:30:01
【问题描述】:

我使用的是 PyCharm,Python 版本是 3.8

我在尝试打印时收到以下错误,例如浮点和变量为 3.14+2.17 的点后 5 位:

print(f'{test:.5f}')
                      ^
SyntaxError: invalid syntax

Process finished with exit code 1

对应的代码是:

test = 3.14 + 2.17
print(test)
print(f'{test:.5f}')

您知道为什么会发生这种情况吗,通过将“f”更改为“format”,问题仍然存在。我将 Python Interpreter 显式更改为 3.8 并删除了 2.8,以便语法中也可以接受“f”。

谢谢。

附言我已经在www.Repl.it 网站上检查了以下代码,并且打印工作正常,所以问题出在我的设置中......:

test = 3.14+2.17
print(test)
print(f'{test:.5f}')

【问题讨论】:

  • 您能否通过在您的代码中运行import sys; print (sys.version) 来检查实际版本并告诉我们输出是什么?
  • 我已经运行了它,使用的版本是.2.72...我已经从添加/删除程序中删除了它,现在只有 3.8.3 是 Python 剩下的一个版本。但是,PyCharm 代码无法再执行,虽然选择的解释器是 3.8... 我现在将研究如何将 Python 安装链接到 PyCharm。
  • 最烦人的问题类型:P 祝你好运!
  • @Adam.Er8 我已经重新安装了 PyCharm 并将其绑定到最新的 Python 解释器,现在“f”运算符按预期工作 - 感谢您的帮助!

标签: python-3.x floating-point


【解决方案1】:

尝试像这样兄弟:)

test = 3.14+2.17
print(test)
print(f'{round(test, 5)}')

但是,更好...

test = 3.14+2.17
print(test)
print(round(test, 5))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多