1. 程式人生 > >python自學筆記1-求1-100的和

python自學筆記1-求1-100的和

奇數 tro .... str 偶數 art pytho break true

求1-100的和

>首先定義一個索引變量
start = 1
>定義一個變量記錄每次的的和
sum = 0
while True:
print(start)
sum = sum + start
print(sum)
if start == 100:
break
start += 1

輸出1-100的內所有奇數
start = 1
while True:
if start % 2 != 0:
print(start)
if start == 100:
break
start += 1
輸出1-100的內所有偶數
start = 1
while True:
if start % 2 == 0:
print(start)
if start == 100:
break
start += 1
求1-2+3-4+5-6.....99 所有數的和
start = 1
sum = 0
while True:
if start % 2 == 0:
sum = sum - start
else:
sum = sum + start
print(sum,start)
print(start)
if start == 99:
break
start += 1
 
 

python自學筆記1-求1-100的和