Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to print the sum of numbers between 0 to 5 but it is giving an error. Please check and help me. I have tried changes in if condition but the issue is same. Kindly assist me, please

What I have tried:

<pre>#! python3 

def function(n):
    if n == 0:
        return 0
    else:  
        return sum + function(n - 1)
        


n = 5 

print(function(n))
Posted
Updated 4-Jul-21 20:50pm

1 solution

sum is undefined.
Try
Python
def function(n):
    if n == 0:
        return 0
    else:
        return n + function(n - 1)

n = 5
print(function(n))
 
Share this answer
 
Comments
MOHAMMED BILAL NADEEM 5-Jul-21 3:06am    
Thanks a lot!
CPallini 5-Jul-21 3:16am    
You are welcome.
Glad to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900