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

计算1+……+100中偶数和

时间:2017-07-16 13:38:18      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:usr   ==   pytho   name   如何   strong   soft   count   return   

如何计算1+……+100中偶数和?
  1. 把奇数去掉,通过if,判断累加数除以2的余数,是否为1,判断是否是奇数
  2. 通过continue 跳过对奇数的累加
#!/usr/bin/python3


def sum_go(sum_to):
    ‘‘‘计算1+……+100中偶数和‘‘‘
    count = 0
    sum_all = 0
    while count <= sum_to:
        count += 1
        if count % 2 == 1:
            continue
        else:
            print(count)
            sum_all += count
    return sum_all

if __name__ ==‘__main__‘:
    result = sum_go(sum_to=100)
    print(‘结果是:%s‘ % result)

  

计算1+……+100中偶数和

标签:usr   ==   pytho   name   如何   strong   soft   count   return   

原文地址:http://www.cnblogs.com/2bjiujiu/p/7190258.html

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