python while、continue、break
while循环实现用户登录
_user = "tom" _passwd = "abc123" counter = 0 while counter < 3: #当条件为假时,跳出循环 username = input("Username:") password = input("Password:") if username == _user and password == _passwd: print("welcome %s login...." % _user) break # 跳出循环 else: print("Invalid username or password!") counter += 1 if counter == 3: keeping_going = input("还想继续吗?[Y/N]") if keeping_going == "Y": counter = 0 else: print("已经错三次了,双双小可爱")
continue和break:
continue # 结束本次循环,进行下次循环
Break # 跳出整个当前循环
实例程序:
exit_flag = False for n in range(10): if n < 5: continue print(n) for N in range(10): print("layer2", N) if N == 6: exit_flag = True break if exit_flag: break
输出结果:
5 layer2 0 layer2 1 layer2 2 layer2 3 layer2 4 layer2 5 layer2 6