Python学习-while循环练习

发布时间:2019-03-19 21:10:53编辑:auto阅读(2406)

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

      

关键字