+ 1

Why it returned 'None'? [PS : "Was trying recursion"]

arr = [1,2,3,4,5] def rec(arr): count = 0 if min(arr) == max(arr): return count else: for i in range(len(arr)-1): arr[i]+=1 count+=1 arr.sort() If min(arr)!=max(arr): return rec(arr) arr = [1,2,3,4,5] print(rec(arr)) Why it gives 'None' as an output, I don't know if my code is correct as i just started learning recursion

8th Apr 2020, 11:38 AM
Amit Dubey
Amit Dubey - avatar
3 Answers
+ 3
You have if min(arr) != max(arr): statement near the end of your function. But you don't return anything if they do equal each other. That's when it returns None. Otherwise, for your first attempt using recursion, you've done well! 😉
8th Apr 2020, 11:56 AM
Russ
Russ - avatar
+ 1
Thanks for the help, silly mistake tho!
8th Apr 2020, 12:00 PM
Amit Dubey
Amit Dubey - avatar
+ 1
Amit Dubey Don't beat yourself up about it. We've all made them and we will all continue to make them!
8th Apr 2020, 12:02 PM
Russ
Russ - avatar