r/pythonhelp • • Oct 16 '20

Help

If someone chose the number 3, how would I make a code that chooses all the positive integers from 1 to n (3), and displays the sum of the numbers? So for example 3, 1+2+3=6(sum)

4 Upvotes

3 comments sorted by

View all comments

1

u/drollia Oct 16 '20

You are going to want to use the built-in functions, Range() , sum() and input ()

pseduocode:

var_1 = input("please enter a number")
var_2 = int(var_1)

range(1, var_2)

That should get you the range with the number from 1 to n. Then you will just to sum the numbers. This might be a good reference for summing a range: https://stackoverflow.com/questions/20455977/sum-up-all-the-integers-in-range