为什么print('\ n')* 100在Python 3.3中不起作用?

时间:2012-12-06 19:06:33

标签: python-3.x python-2.7

我正在尝试清除Python中的代码行,并在Any way to clear python's IDLE window?发布了关于如何执行此操作的帖子,但是当我在IDLE 3.3中运行下面的函数时,我得到以下错误。但它确实适用于2.7.3版本。

错误

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    cls()
  File "<pyshell#6>", line 2, in cls
    print('\n') * 100
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

CODE

def cls():
    print('\n') * 100

1 个答案:

答案 0 :(得分:5)

你可能意味着

print('\n' * 100)

将字符串乘以int时,会重复:

>>> 'ha' * 3
'hahaha'

但您所做的是将print('\n')的值乘以100。但print()不返回任何内容(读取:返回None),因此错误:您无法将Noneint相乘。

在Python 2中没有区别,因为没有括号:

print '\n' * 100

但是,Python的解释方式与Python 3相同(并不像你想象的那样)。

相关问题