前言:python的break会终止整个程序,但是continue只会终止循环,让我们一起看看吧.
求0-100的奇数之和
#!/usr/bin/env python
counter = 0
sum100 = 0
while counter < 100:
counter += 1
if counter % 2 == 0:
continue
sum100 += counter
print sum100
求0-100的偶数之和
#!/usr/bin/env python
counter = 0
sum100 = 0
while counter < 100:
counter += 1
if counter % 2: //前边还可以加not,计算的就是奇数和,非0为真
continue
sum100 += counter
print sum100
注:python非空为真,非0为真
时间: 2024-10-07 20:08:41