Python中的while循环

zhuxianfeng 2020-02-09

num=1
while num<=10:
    print(num)
    num+=1结果为1,2,3,4,5,6,7,8,9,10

num=1
while num<=10:
    num+=1
    print(num)

结果为2,3,4,5,6,7,8,9,10,11

输出1-100之间的偶数:

方法一:

1 num=22 while num<=100:
3     print(num)
4     num+=2

方法二:

num=1
while num<=100:
    if num%2==0:
        print(num)
    num+=1

输出1-100之间所有的奇数:

num=1
while num<=100:
    if num%2==1:
        print(num)
    num += 1

 

相关推荐