'''从键盘录入密码,最多录入3次,如果正确就结束循环'''print("---------------------for 循环-------------------------")for i inrange(3):
pwd =input("请输入密码:")if pwd =="8888":print("密码正确")break# 退出循环else:print("密码错误")print("---------------------while 循环-------------------------")
a =0while a <3:
pwd =input("请输入密码:")if pwd =="8888":print("密码正确")breakelse:print("密码错误")
a +=1
运行结果:
D:\Environment\Python\Python311\python.exe D:\Environment\PythonWorks\learnpython\马士兵Python\第5章_转圈圈\P41_40.流程控制语句break.py
---------------------for 循环-------------------------
请输入密码:8888
密码正确
---------------------while 循环-------------------------
请输入密码:1234
密码错误
请输入密码:6783
密码错误
请输入密码:3456
密码错误
Process finished with exit code 0