HOW CAN I WRITE A PROGRAM THAT CALCULATES THE FIRST N NATURAL NUMBERS THAT USER ENTERS. I SHOULD USE FUNCTIONS.

n = eval(input("Enter the value of n: "))

def sumN(n):
for i in range(n):
sum = n+i
return sum

sum = sumN(n)

THIS IS WHAT I DID SO FAR. THANKSSS

TrustyTony commented: 20 posts and you still do not use code tags! -3

Recommended Answers

All 2 Replies

HOW CAN I WRITE A PROGRAM THAT CALCULATES THE FIRST N NATURAL NUMBERS THAT USER ENTERS. I SHOULD USE FUNCTIONS.

THIS IS WHAT I DID SO FAR. THANKSSS

n = eval(input("Enter the value of n: "))

def sumN(n):
    for i in range(n):
        sum = n+i
        return sum

sum = sumN(n)

Learn to use code tags, push (CODE) right before paste.

Why you use for in sumN, as it allways return in first loop?

yea it is suppose to be

n = eval(input("Enter the value of n: "))

def sumN(n):
sum = 0
for i in range(n):
sum = sum+i
return sum

sum = sumN(n)

print(sum)

Thanks !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.