python的while循環輸出數字

a. 使用while循環實現輸出2-3+4-5+6...+100 的和

# 定義計算結果
aaa = ''
bbb = 1
#for i in range(1, 100):
i = 1

while i < 100:
    i += 1
    aaa += str(i)
    if i % 2 == 0:
        aaa += '-'
        bbb += i
    else:
        aaa += '+'
        bbb -= i

print('字符串輸出: \r\n %s \r\n計算結果: \r\n %s' %(aaa.rstrip('-'),bbb))


b. 使用 while 循環實現輸出 1,2,3,4,5, 7,8,9, 11,12 使用 while 循環實現輸出 1-100 內的所有奇數
##輸出1--12

count=1
while count <= 12:
    if count == 6 or count == 10:
        count += 1
        continue #跳出本次循環
    print(count)
    count+=1

##   輸出1--100之間的所有奇數

count=0
while count <= 100:
    if count%2 == 1:
        print(count)
    count+=1
e. 使用 while 循環實現輸出 1-100 內的所有偶數

##   輸出1--100之間的所有奇數

count=0
while count <= 100:
    if count%2 == 0:
        print(count)
    count+=1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章