从python中的范围计算用户输入数字

counting user input numbers from range in python

本问题已经有最佳答案,请猛点这里访问。

所以我的问题是,这个程序应该采用输入的数字,例如5是0+1+2+3+4,和是10。我已经得到它列出这些数字,但我该如何使这个代码来计算它们?

1
2
3
4
5
6
num1 = int(input("How many laps?:"))
num2 = int(0)

for lap in range (num1):
   num2 = lap
   print("Sum is:" , (num2))

谢谢


像你试过的那样做

1
2
3
4
5
6
7
8
9
10
num1 = int(input("How many laps?:"))

list = []

for lap in range (num1):
   list.append(lap)

listSum = sum(list)

print(listSum)

简单的解决方法如下

1
2
num1 = int(input("How many laps?:"))
print(sum(range(num1)))