码迷,mamicode.com
首页 > 其他好文 > 详细

练习—8.17 while循环

时间:2017-09-11 00:55:20      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:art   else   之间   start   方法   int   个数   打印   ==   

练习题:

1. 打印 1-99之间的所有奇数(循环,每个数递增)

方法一:

start = 1
while start <= 99:
     n = start % 2
     if n == 1:
        print(start)
        start = start + 1
     else:
        start = start + 1

方法二:

start = 1
while start <= 99:
     n = start % 2
     if n == 1:
        print(start)
     start = start + 1

2. 打印 1-99之间所有能被3整除数

start = 1

while start <= 99:
     n = start % 3
     if n == 0: 
        print(start)
     start = start + 1

3. 打印 1-99之间所有数总和

start = 1
num = 0
  while start <= 99:
     num = num + start
     start = start + 1
  print(num)

4. 打印 1-99之间所有奇数总和
start = 1
num = 0
while start <= 99:
     n = start % 2
     if n == 1:
        num = num + start
   start = start + 1
  print(num)

5. 打印 1-2+3-4+5-6+7 ...+99  总和

start = 1
num = 0
 while start <= 99:
     n = start % 2
     if n == 1:
        num = num + start
     else:
        num = num - start
     start = start + 1

print(num) 
   

练习—8.17 while循环

标签:art   else   之间   start   方法   int   个数   打印   ==   

原文地址:http://www.cnblogs.com/xu1686318405/p/7502952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!