Python Forum
Need some help with a simple syntax mystery
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need some help with a simple syntax mystery
#1
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
Reply
#2
you can't add print to print.
print('Lijn 1\nLijn 2\nLijn 3\n', int('2') + int('1'))
99 percent of computer problems exists between chair and keyboard.
Reply
#3
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
Reply
#4
or
print('Lijn 1\nLijn 2\nLijn 3'); print(int('2') + int('1'))
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple syntax to asynchronously get access to MODBUS register orion67 1 2,848 Jan-22-2022, 12:40 PM
Last Post: orion67
Shocked An array "mystery": The same array, the same operations but different outcomes mewss 3 2,147 Feb-17-2021, 06:34 PM
Last Post: mewss
  sorted function example mystery sabaidii2 4 2,557 Feb-10-2020, 09:37 AM
Last Post: DeaD_EyE
  simple syntax question speedskis777 7 3,596 Mar-11-2019, 02:26 AM
Last Post: snippsat
  Reference counting mystery beezergeezer 4 2,957 Jul-24-2018, 02:37 PM
Last Post: beezergeezer
  Can't figure out the syntax error, relatively simple code maho686868 3 3,139 Jul-08-2018, 03:43 PM
Last Post: volcano63
  Help with mystery data paulr 4 4,321 Sep-07-2017, 08:02 AM
Last Post: paulr
  Simple syntax error help paulmj7 4 3,585 Aug-02-2017, 09:56 AM
Last Post: buran
  Syntax error for simple script pstein 2 4,004 Jun-26-2017, 03:46 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020