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