Python學習-while迴圈練習

瞳孔冷色發表於2018-10-20

1.計算1-100的和

i = 1;
total = 0;
while i <= 100:
        total = total + i;
        i = i + 1;
print(total);

2.列印出1-100的奇數

i = 0;
while i<= 100:
        temp = i % 2;
        if temp == 0:
                pass;
        else:
                print(i);
        i = i + 1;
else:
        print("程式執行結束!");

3.列印5遍“hello world”

num = 0;
n = True;
while n:
        print(`hello world`);
        num = num + 1;
        if num == 5:
                n = False;

4.計算1+2-3……+100的和

i = 1;
total = 0;
while i <= 100:
        temp = i % 2;
        if  temp == 0:
                total = total - i;
        else:
                total = total + i;
        i = i + 1;
print(total);

  

相關文章