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

  

猜你喜欢

转载自www.cnblogs.com/pcliu/p/9819836.html
今日推荐