count = 0 while count <= 5 : count += 1 if count == 3:break print("Loop",count) else: print("循环正常执行完啦") print("-----out of while loop ------")
当while中有break语句打断时,else不会执行。
当while中没有被break语句打断时,else就会执行。
username='123' password='123' i=0 while i<3: name=input('input your name:') pwd=input('input your password:') if name==username and password ==pwd: print('login successful!') break else: print(f'login failed,more {2-i} times.') if (2-i)==0: result=input('Retry? Y/N?') if result == 'Y' or result =='y': i=0 continue i+=1 print('NO chances!')
Python登录验证与循环控制
本文探讨了Python中while循环的使用,包括如何利用break语句提前终止循环,以及else子句在循环正常结束时的执行。同时,通过一个简单的登录验证系统示例,展示了循环在实际应用中的灵活性。
1562

被折叠的 条评论
为什么被折叠?



