Python学习-while循环练习

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);

  

posted @ 2018-10-20 13:18  瞳孔冷色  阅读(891)  评论(0编辑  收藏  举报