- ctrl+c结束死循环
while True:
print('finished!')
break
- break 退出循环
- while+if判断+break中断循环
求1-100的整数和
x = 0
s = 0
while x<=100:
s = s+x
x = x+1
print(s) #结果为5050
x = 0
s = 0
while True:
s = s + x
x = x + 1
if x>100:
break
print(s) #结果为5050
- continuwe继续循环,跳过当前循环,进入下一循环。
创建一个1-50的列表,求和其中的所有奇数
lst = list(range(1,51))
s = 0
for i in lst:
if i%2 != 0:
s = s + i
else:
continue
print(s) #结果为625