- 用for循环语句
sum = 0
for x in range(101):
sum += x
print(sum)
2,用while循环语句
n = 100
sum = 0
counter = 1
while counter <= n:
sum = sum + counter
counter += 1
print('1到%d之和为:%d'%(n,sum))
或者
sum = 0
counter = 1
while counter <= 100:
sum = sum + counter
counter += 1
print(sum)