给字符错误的f字符串?

Ale*_*man 10 python syntax-error python-2.x f-string

我在这里收到了我的Atom阅读器的错误消息,其中建议第一个print.(f"message")是发送错误:

File "/Users/permanentmajority/Desktop/Coding/learnpythonbook.py", line 75
    print(f"Let's talk about {my_name}.")
                                       ^
SyntaxError: invalid syntax
[Finished in 0.077s]
Run Code Online (Sandbox Code Playgroud)

码:

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 #lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print(f"Let's talk about {my_name}.")
print(f"He's {my_height} inches tall.")
print(f"He's {my_weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {my_eyes} eyes and {my_hair} hair.")
print(f"His teeth are usually {my_teeth} depending on the coffee.")
Run Code Online (Sandbox Code Playgroud)

InA*_*ash 14

我想你有一个旧版本的python.尝试升级到最新版本的python.自python 3.6以来,f-string文字已添加到python中.你可以在这里查看更多相关信息


小智 9

这是python版本问题。

而不是使用

print(f"Let's talk about {my_name}."
Run Code Online (Sandbox Code Playgroud)

print("Let's talk about {}.".format(my_name))
Run Code Online (Sandbox Code Playgroud)

在python2中。

您的代码适用于 python3.7。

在这里查看:

my_name= "raushan"
print(f"Let's talk about {my_name}.")
Run Code Online (Sandbox Code Playgroud)

https://repl.it/languages/python3


小智 5

Python 解释器会导致以下问题,因为执行程序时调用的 python 版本错误,因为 f 字符串是 python 3 的一部分,而不是 python 2。\xc2\xa0你可以这样做python3 filename.py,它应该可以工作。要解决此问题,请将 python 解释器从 2 更改为 3。\xc2\xa0

\n


归档时间:

查看次数:

16205 次

最近记录:

6 年,3 月 前