相关疑难解决方法(0)

尝试执行字符串插值时,Python 3返回"无效语法"

我最近一直在学习python 3,我无法得到任何涉及字符串插值(格式化)的例子.

In [1]: state = "Washington"

In [2]: state
Out[2]: 'Washington'

In [3]: my_message = f"I live in {state}"
File "<ipython-input-3-d004dd9e0255>", line 1
my_message = f"I live in {state}"
                                ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

我认为我的机器默认为python 2,但快速检查显示:

Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
Type "copyright", "credits" or "license" for more information.

IPython 5.2.2 -- An enhanced Interactive Python.
Run Code Online (Sandbox Code Playgroud)

我在Ubuntu 16.04:

python3 --version
Python 3.5.2
Run Code Online (Sandbox Code Playgroud)

我只是忽略了基本语法?我在同学的几台计算机上运行相同的命令,似乎执行得很好.

python string-interpolation python-3.x ubuntu-16.04

24
推荐指数
2
解决办法
3万
查看次数

给字符错误的f字符串?

我在这里收到了我的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 …
Run Code Online (Sandbox Code Playgroud)

python syntax-error python-2.x f-string

10
推荐指数
3
解决办法
2万
查看次数