输出1-100中所有的奇数或者偶数

num = 0
while num < 100:
    num += 1
    if num % 2 == 1:
        print(num)

所有的奇数

num = 0
while num < 100:
    num += 1
    if num % 2 == 0:
        print(num)

所有的偶数

num = 0
con = 0

while num < 99:
    num += 1
    if num % 2 == 0:
        con = con - num
    elif num % 2 == 1:
        con += num
print(con)

求1-2+3-4.。。。。。+99的和

i = 0
sum = 0
while i < 100:
    if i == 88:
        pass
    elif i % 2 !=0:
        sum += i
    else:
        sum = sum - i
    i += 1
print(sum)

求1-2+3-4.。。。。。+99的和除去88改良版

教学

i = 0
sum = 0
while i < 100:
    if i == 88:
        i +=1
        continue
    elif i % 2 !=0:
        sum += i
    else:
        sum = sum - i
    i += 1
print(sum)

 

posted @ 2018-11-21 13:48  20181235  阅读(411)  评论(0编辑  收藏  举报