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
3 Respuestas
0
Use range() and a for-loop.
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))
0
N = int(input())
sum = 0
for i in range(N+1):
sum += i
print(sum)
print(int(N + 1)*N / 2))