我在使用Python 3.7.
我在第3行的代码工作正常,但是当我将基础公式插入第4行时,我的代码返回错误:
SyntaxError:f-string:mismatched'(','{'或'[' (错误指向第一个'('第4行'.
我的代码是:
cheapest_plan_point = 3122.54
phrase = format(round(cheapest_plan_point), ",")
print(f"1: {phrase}")
print(f"2: {format(round(cheapest_plan_point), ",")}")
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚第4行有什么问题.
我喜欢 python 3.6 中的新 f-Strings,但是在尝试在表达式中返回 String 时我看到了一些问题。下面的代码不起作用并告诉我我使用了无效的语法,即使表达式本身是正确的。
print(f'{v1} is {'greater' if v1 > v2 else 'less'} than {v2}') # Boo error
Run Code Online (Sandbox Code Playgroud)
它告诉我,'greater'并且'less'是意料之外的标记。如果我用两个包含字符串的变量甚至两个整数替换它们,错误就会消失。
print(f'{v1} is {10 if v1 > v2 else 5} than {v2}') # Yay no error
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?