各大网站平台发布信息,网站游戏网站开发设计菲律宾,做网站都需要什么软件,wordpress 打开满for循环用于针对序列中的每个元素的一个代码块。
while循环是不断的运行#xff0c;直到指定的条件不满足为止。
while 条件#xff1a; 条件成立重复执行的代码1 条件成立重复执行的代码2 ……..
i 1while i 5:print(i)i i 11、使用wh…for循环用于针对序列中的每个元素的一个代码块。
while循环是不断的运行直到指定的条件不满足为止。
while 条件 条件成立重复执行的代码1 条件成立重复执行的代码2 ……..
i 1while i 5:print(i)i i 11、使用while循环打印1~100的所有数字
2、计算1-100内的累加如1234…100
3、计算1-100的偶数累加和 如246…100 i 1
while i 100:print(i)i i 1x 1
y 0
while x 100:y y xx x1
print(1 到 100 的累加和为:, y)x 2
y 0
while x 100:y y xx x 2
print(1 到 100 的偶数累加和为:, y) 退出循环的两种不同方式
break 终止循环
continue 退出本次循环继续下一次循环 1、一共5个苹果吃到第4个时吃饱了不吃了。
total_apples 5
eat_count 0while eat_count 4:eat_count 1print(吃了第, eat_count, 个苹果)print(吃饱了不吃了)2、一共5个苹果吃到第3个时发现一只大虫子第3个不吃了继续吃第4个苹果直到吃完
total_apples 5
eat_count 0while eat_count total_apples:eat_count 1if eat_count 3:print(第, eat_count, 个苹果有虫子不吃了)continueprint(吃了第, eat_count, 个苹果)print(吃完了所有的苹果)