我正在编写一个简单的打印语句。但是我在atom

问题描述 投票:0回答:1
中收到语法错误

[请注意,此代码直接来自python书籍(eric mathes撰写的第2版python崩溃课程)。当我运行代码时,它会通过常规的python 3.7.5进行编译。但是,当我尝试通过原子编辑器编译代码时,它不会编译。

我已经为原子编辑器安装了许多python软件包,包括python片段,python工具,run-python-simply。但这似乎还没有编译。

first_name = "Adam"
last_name = "Scott"
full_name = f"{first_name}{last_name}"
print(full_name)

File "/Users/Adam/Desktop/full_name.py", line 3
    full_name = f"{first_name}{last_name}"
                                         ^
SyntaxError: invalid syntax
python atom-editor
1个回答
1
投票

您只能在Python 3.6(包括)之后使用f字符串,以便更好地检查版本,或者可以使用较旧的方法format()获得相同的输出。

first_name = "Adam"
last_name = "Scott"
full_name = "{0} {1}".format(first_name, last_name)
print(full_name)

Adam Scott
>>>

0
投票

请检查您的Atom编辑器中配置了哪个python版本。上面的语法在python 3.6版之后使用。这些是f-strings。无论您使用的是哪个python版本,一种更好的方法都是使用字符串格式。以下是相同的语法。

first_name = "Adam"
last_name = "Scott"
full_name = "{} {}".format(first_name,last_name)
# the string formatting works well with any version of python
# you can pass indexes as mentioned by Deepak Dixit
print(full_name)
© www.soinside.com 2019 - 2024. All rights reserved.