循环

while-break-continue
while True:
    s = input("请输入:")
    if s == 'q':
        break  # 停止当前循环,跳出循环
    if "马化腾" in s:
        print("输入的内容不合法")
        continue  #停止当前本次循环,继续执行下一次循环
    print(s)

while 条件:
代码块(循环体)
执行流程:
1.判断条件是否为真,如果为真,执行代码块
2.再次判断条件是否为真
3.当条件为假,跳出循环,循环结束

while-break-else
count = 1
while count <= 10:
    print(count)
    count = count + 1
    if count == 5:
        break  # 彻底停止循环,不会执行后面的else
else:  #while条件不成立的时候执行
    print("这里是else")
posted on 2025-06-26 16:52  搁浅芳  阅读(5)  评论(0)    收藏  举报