0

help with python 33 code project

Project question is: Take a number N as input and output the sum of all numbers from 1 to N (including N). Given code is N = int(input()) I don’t know where to go from there? Please help. I need to turn N into a list first, but I’m not sure how to do that

4th Sep 2021, 9:13 PM
Iso_Aku
3 ответов
0
Use range() and a for-loop.
4th Sep 2021, 9:19 PM
Simon Sauter
Simon Sauter - avatar
0
Here are a few solutions: N = int(input()) # You could iterate over the range of 0...N and aggregate the sum. sum = 0 for i in range(N + 1): # loop i over 0...N inclusive sum += i print(sum) # You could optimize down to the following expression: print(int((N + 1) * N / 2))
4th Sep 2021, 9:24 PM
Josh Greig
Josh Greig - avatar
0
N = int(input()) sum = 0 for i in range(N+1): sum += i print(sum) print(int(N + 1)*N / 2))
26th Aug 2022, 1:09 AM
Rohit Koli