格式语法无效

Invalid syntax for format

Python 3.4 给出了此语句的错误。这是 o 打印用于模糊逻辑运算的笛卡尔积。

 print(f'The size of the relation will be: {len(self)}x{len(other)}')

我正在尝试对模糊集执行并集、交集和差集运算。

正如@hiroprotagonist 在评论中提到的,f 弦是在 Python 3.6 中添加的。对于 Python 3.x 之前的 Python 3.6,改为执行此操作:

print('The size of the relation will be: {0}x{1}'.format(len(self), len(other)))

或使用旧式 Python 2 格式(尽可能不要使用此格式):

print('The size of the relation will be: %dx%d' % (len(self), len(other)))

python 3.4 不支持 f-strings 如果需要,您可以模拟它们:

def f(s):
    return s.format(**globals())

answer = 'CRICKET'
print(f('FOOTBALL OVER {ans}'))

编辑: 最好使用 .format 因为这不能直接用于 len(something)