格式语法无效



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

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

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

正如@hiroprotagonist评论中提到的,在Python 3.6中添加了f-string。对于 Python 3.6 之前的 Python 3.x,请改为执行以下操作:

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 字符串如果需要,您可以模拟它们:

def f(s):
    return s.format(**globals())
answer = 'CRICKET'
print(f('FOOTBALL OVER {ans}'))

编辑:最好使用 .format,因为这不会直接适用于 len(某物)

最新更新