Sum 1 to n python

I'm trying to write a program to add up number from 1 to n. I've managed to get it to print the numbers several times but not add them all. It keeps on just adding two of the numbers.

My 1st attempt is:

def problem1_3[n]:
    my_sum = 0
    # replace this pass [a do-nothing] statement with your code
    while my_sum  0]:.
sum += num..

How do you find the sum of 1 to 100 in Python?

The sum function can be used to calculate the sum of the numbers in the range..
Pass 1 and 100 + 1 to the range class, e.g. range[1, 100 + 1] ..
Pass the range object to the sum[] function..
The sum function will sum the integers from 1 to 100..

How do you add all the numbers from 1 to 10 in Python?

“print sum of all the numbers 1 to 10 in python using for loop” Code Answer's.
n = input["Enter Number to calculate sum"].
n = int [n].
sum = 0..
for num in range[0, n+1, 1]:.
sum = sum+num..
print["SUM of first ", n, "numbers is: ", sum ].

Chủ Đề