用while循环处理1-100内之和,1-100内的所有奇数,1-100内所有偶数,1-2+3-4+...99的和...

1-100内求和

count = 1
sum = 0
while count <= 100:
    sum = sum + count
    count +=1
print(sum)
1-100内的所有奇数

count = 1
while count < 100:
    if count % 2 != 0:
        print(count)
    count += 1
1-100内所有偶数

count = 1
while count < 100:
    if count % 2 == 0:
        print(count)
    count += 1
1-2+3-4...99的和

count = 1
sum = 0
while count <= 99:
    if count % 2 != 0:
        sum += count
    else:
        sum -= count
    count += 1
print(sum)

 

转载于:https://www.cnblogs.com/Little-Raccoon/p/10754884.html

你可能感兴趣的:(用while循环处理1-100内之和,1-100内的所有奇数,1-100内所有偶数,1-2+3-4+...99的和...)