0

I am attempting to clear lines of code in Python and came across a post at Any way to clear python's IDLE window? on how to do so however when I run the function below in IDLE 3.3 I get the error below. It does however work in version 2.7.3.

ERROR

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
Community
  • 1
  • 1
PeanutsMonkey
  • 6,919
  • 23
  • 73
  • 103

1 Answers1

6

You probably mean

print('\n' * 100)

When you multiply a string by an int, it is repeated:

>>> 'ha' * 3
'hahaha'

But what you do is multiply the value of print('\n') by 100. But print() doesn't return anything (read: returns None), hence the error: you can't multiply None and int.

In Python 2 there is no difference, because there are no parentheses:

print '\n' * 100

Still, it's interpreted by Python the same way as in Python 3 (and not the same way you seem to iterpret it).

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • But isn't `NoneType` a string value? If so why can't it multiply `None * 100` – PeanutsMonkey Dec 06 '12 at 19:16
  • 1
    @PeanutsMonkey No, [`NoneType` is a special type](http://docs.python.org/2/library/stdtypes.html#the-null-object). There is only one object of this type and it's `None`. Strings have type `str`. Try `type(None)` and `type('hello')` in Python shell, for example. You need to distinguish `None` from the string `"None"`, of course. – Lev Levitsky Dec 06 '12 at 19:18