python-while else
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!')