while ----else------
while后面else的作用是指,当while循环正常执行,中间没有被break终止的话,就会执行else后面的语句
count = 0 while count <= 5: count += 1 print("loop ",count) else: print("循环正常执行完毕")
如何判断while循环是否被终止过,可以通过添加flag
count = 0 flag = 0 while count < 5: print("loop ",count) if a == 3: flag = 1 break count += 1 else: print("循环正常执行完毕") if flag == 1: print("执行了break")