Def sum(a,b): If a==0 or b==0: Return 0: Return 1+sum(a-1,b-1): Print (sum(4,2)) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Def sum(a,b): If a==0 or b==0: Return 0: Return 1+sum(a-1,b-1): Print (sum(4,2))

From python

9th May 2020, 5:15 PM
Colonel Cop
Colonel Cop - avatar
10 Answers
+ 3
Don't add colon (:) for return statements.
9th May 2020, 5:37 PM
akamhy
akamhy - avatar
+ 2
I took your code and tried to understand the logic of if. Here is a reworked version: def sum_(a,b): if a==0 or b==0: return 0 else: return 1+sum_(a-1,b-1) print(sum_(4,2)) Please compare it with your code to find the amendments. Colonel Cop, I would recommend you to go through the python tutorial to get the basics. It' really worth to work on it.
9th May 2020, 5:38 PM
Lothar
Lothar - avatar
+ 1
Ok - so what is your question, or how can we help you? [ Edited]: There should no python function names and so on be used as identifiers. This can cause unexpected errors or results. Also indentation should be done properly.
9th May 2020, 5:17 PM
Lothar
Lothar - avatar
0
This is my question on Python 3
9th May 2020, 5:19 PM
Colonel Cop
Colonel Cop - avatar
0
Then please explain step wise for that reworked version
9th May 2020, 5:51 PM
Colonel Cop
Colonel Cop - avatar
0
Please
9th May 2020, 5:52 PM
Colonel Cop
Colonel Cop - avatar
0
Col. Any experience on recursion? Calling a function within itself? If not, let me tell you that you're using recursion here.
9th May 2020, 5:55 PM
akamhy
akamhy - avatar
0
No
10th May 2020, 1:08 AM
Colonel Cop
Colonel Cop - avatar
0
Experience
10th May 2020, 1:08 AM
Colonel Cop
Colonel Cop - avatar
- 3
Def sum(a,b): If a==0 or b==0: Return 0: Return 1+sum(a-1,b-1): Print (sum(4,2))
9th May 2020, 5:19 PM
Colonel Cop
Colonel Cop - avatar