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

如何计算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)

  

上一篇:清除Cookie、获取指定Cookie的值、添加一个Cookie(24小时过期)、添加一个Cookie


下一篇:invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun