Why this code is giving output 2even if the function is not called ???? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why this code is giving output 2even if the function is not called ????

def print_numbers() print(1) print(2) return print(3)

11th Nov 2016, 12:04 PM
PAWAN KUMAR
PAWAN KUMAR - avatar
6 Answers
+ 2
check it now
11th Nov 2016, 12:09 PM
PAWAN KUMAR
PAWAN KUMAR - avatar
+ 2
In addition to indentation, there's no trailing colon (:) on the function definition.
11th Nov 2016, 12:25 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
the same code was given in the functions section as question and the answer was 2 ... how????
11th Nov 2016, 12:29 PM
PAWAN KUMAR
PAWAN KUMAR - avatar
+ 2
Hey...those details are important. It can be frustrating getting them all but it really helps (us and you). I went to the "functions section" but it wasn't there (others might have looked too, not found it and left). I found it as the last question in a later block: "Returning from Functions". The function above isn't the same...well, almost...but distracting because it's not indented properly (a plausible reason for returning 2 + crash) possibly also if the missing : is a typo. You indicate "even if the function is not called" when the question is: "What is the highest number this function prints if called?" It's 2 in the question because when you imagine the function is called, the return statement ends the indented block, and the next two lines (where you show 1) are ignored. If you have a copy of the code like the above, it's 2 for you probably because the 'return' statement stops the interpreter...but not before it prints, assuming a typo at 'def' or something...when it 'falls through' the code without indent hints. I'm sure about the first reason, not so sure about the second.
12th Nov 2016, 12:49 AM
Kirk Schafer
Kirk Schafer - avatar
+ 1
Did you forget to indent the code?
11th Nov 2016, 12:12 PM
John Doe
0
where is the code
11th Nov 2016, 12:06 PM
P Sandesh Baliga
P Sandesh Baliga - avatar