About your code:
if y == 1:
return y
elif y <= 1 and y <= 999999999:
print(sum(y))
First, why are you using return
in if
part but using print()
in elif
part?
Then, simply do something like this:
def function_name(number):
return sum(range(1, number+1))
Demo:
For example, the number is 10
:
print(function_name(10))
Output:
55
range()
will give a generator like 1, 2, 3...8, 9, 10
here. And sum()
can do something like 1 + 2 + 3...8 + 9 + 10
that you want.
8
solved Python 3.4, add numbers up to an integer [duplicate]