+ 11

1). explain it to me I do not understand

What is the highest number output by this code? def print_nums(x): for i in range(x): print(i) return print_nums(10)

10th Sep 2020, 4:44 AM
Shubham đŸ–€
Shubham đŸ–€ - avatar
8 Answers
+ 5
So, why print_nums(10) is written here
10th Sep 2020, 6:05 AM
Shubham đŸ–€
Shubham đŸ–€ - avatar
+ 4
Thanks a lot for explaining
10th Sep 2020, 8:13 AM
Shubham đŸ–€
Shubham đŸ–€ - avatar
+ 2
But here i not defined
10th Sep 2020, 6:14 AM
Shubham đŸ–€
Shubham đŸ–€ - avatar
+ 2
Ans will be 0 becoz the return statement is in for loop and the loop exits in the first run only
11th Sep 2020, 2:13 AM
Namit Aggarwal
+ 1
So, how we know value
10th Sep 2020, 6:15 AM
Shubham đŸ–€
Shubham đŸ–€ - avatar
+ 1
print_nums (10) Is calling the already defined "print_nums" function on the number "10" Its asking for the result of print_nums(x) if the x is replaced with 10
10th Sep 2020, 12:33 PM
Chukwunonso Emmanuel Chukwumaeze
Chukwunonso Emmanuel Chukwumaeze - avatar
+ 1
Your result will be 0 because: You will be immediately thrown out of a function as soon as the python execute the return statement.
10th Sep 2020, 4:10 PM
Vinay Khatri
Vinay Khatri - avatar
+ 1
((So, why print_nums(10) is written here))) It's because..."print_nums(x)" has been initially defined at the top,... So "print_nums(10)", was meant to input 10 into the "print_nums(x)" function in place of "(x)" but the return statement made it loop only once... And the code has been intentionally made not to be straight -forward
10th Sep 2020, 10:09 PM
Israel Oyetunji
Israel Oyetunji - avatar