Python基础之While else用法

    科技2024-10-12  18

    while 表达式: 语句1 else: 语句2

    实例一:

    a = 1 while a < 3: print("sunck is a good man!") a += 1 else: print("very very good") print("you are right") 结果: sunck is a good man! sunck is a good man! very very good you are right

    实例二:

    a = 1 while a < 3: print("sunck is a good man!") a += 1 if a == 2: break else: print("very very good") print("you are right") 结果: sunck is a good man! you are right

    总结:

    当while中有break的时候不会走else,反之会走else.

    Processed: 0.017, SQL: 8