python求1~100的和

方法一:使用while循环
sum = 0
count = 1
while count<=100:
    sum+=count
    print("%d + %d = %d"%(sum-count,count,sum))
    count += 1
方法二:for循环
sum=0
for i in range(1,101):
    sum+=i
    print("%d + %d = %d"%(sum-i,i,sum))

你可能感兴趣的:(python,算法)