标签:blog for div on log 代码 new as python
continue继续循环
在循环过程中,可以用break退出当前循环,还可以用continue跳过后续循环代码,继续下一次循环。
假设我们已经写好了利用for循环计算平均分的代码:
L = [75, 98, 59, 81, 66, 43, 69, 85] avg = 0.0 sum = 0.0 for num in L: sum+=num avg = sum/len(L) print (‘avg is :‘,avg)
现在如果只想统计及格分数的平均分,就要把 x < 60 的分数剔除掉,这时,利用 continue,可以做到当 x < 60的时候,不继续执行循环体的后续代码,直接进入下一次循环:
L = [75, 98, 59, 81, 66, 43, 69, 85] avg = 0.0 sum = 0.0 newnum = 0 for num in L: if num < 60: continue sum+=num newnum+=1 avg = sum/newnum print (‘avg is :‘,avg)
标签:blog for div on log 代码 new as python
原文地址:http://www.cnblogs.com/xiaoit/p/4042161.html