Python Forum

Full Version: Need some help with a simple syntax mystery
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am just new to Python and learning from scratch. Could anyone please help me solve this equation?

my code is:

>>> print("Lijn 1\nLijn 2\nLijn 3\n") + print(int("1")+ int("2"))
Output:
Lijn 1 Lijn 2 Lijn 3 3
Error:
Traceback (most recent call last): File "<pyshell#215>", line 1, in <module> print("Lijn 1\nLijn 2\nLijn 3\n") + print(int("1")+ int("2")) TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' >>>
So actually the result is there, but why am i getting an error message? is the syntax wrong?
the result i would like to see is:

Lijn 1
Lijn 2
Lijn 3
3

Best regards,
Koen
you can't add print to print.
print('Lijn 1\nLijn 2\nLijn 3\n', int('2') + int('1'))
print is a function and it returns the implicit None.
First the two terms of the addition are evaluated. in the process it execute the print function and the output is what you see. Then it try to evaluate None + None (i.e. the vallues returned by each of the print functions) and because this is not allowed it rise an error
or
print('Lijn 1\nLijn 2\nLijn 3'); print(int('2') + int('1'))