python练习

1.输出 1-100内的所有偶数

 start = 1

while start <101:

    tmp = start % 2

    if tmp == 1:

        pass

    else:

        print(start)

    start += 1

 

2.输出 1-100内的所有奇数

   start = 1
while start <101:
    tmp = start % 2
    if tmp == 1:
        print(start)
    else:
        pass
    start += 1

 

3.求1-2+3…99的所有数的和   

start = 1

sum = 0

while start <100:


    tmp = start % 2
    if tmp == 1:
        sum = sum + start
    else:
        sum = sum -start
    start +=1
print(sum)

4.输入10以内的正整数
start = 1

   flag = True

   while flag:

        print(start)

       if start == 10:

           flag = False

          start += 1

print("end")

 

5.输入除7外的10以内的正整数

start = 1

while True:

    print(start)

    start += 1

    if start == 7:

        start += 1

        continue

    if start == 10:

        start += 1

        break

print("end")

 

6.求1-100以内所有正整数的和

 start = 1

sum = 0

while True:

    sum =sum + start

    if start == 100:

        break

    start += 1

print(sum)

posted @ 2017-03-28 22:11  qianxiamo  阅读(158)  评论(0编辑  收藏  举报