# while 条件: # 循环体 # 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。 # 如果条件为假,那么循环体不执行,循环终止 # count =0 # # while count<5: #0<5 # print(count) # count+=1 # print('顶级代码》----') # 死循环 # count =0 # # while count<5: #0<5 # print(count) # 纯计算的无IO的死循环导致致命的效率问题 # while True: # 1+1 # while 1: # print('aa') # while True: # Name = input('请输入您的账号:') # pwd = input('请输入您的密码:') # if Name=='loky'and pwd =='123': # print('输入正确!登陆成功') # else: # print('输入错误!请重新输入!') # 方式二:break 只要运行到break 就会立刻终止 # while True: # Name = input('请输入您的账号:') # pwd = input('请输入您的密码:') # if Name=='loky'and pwd =='123': # print('输入正确!登陆成功') # break # else: # print('输入错误!请重新输入!') # # print('end!!!!!!!!!') # while的嵌套循环 ''' while True: break while True: break while True: ''' # while +continue :结束本次循环,直接进入下一次循环 # 3强调,在continue后加同级代码豪无意义 # count = 0 # while count < 5: # if count == 4: # continue # print(count) # count += 1 # # # while +else # # while True: # pass # # else: # print('else包含的代码会在while循环结束后,并且while循环赛在没有被break打断的状态下,会输出else!') # 与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句,while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句 # count = 0 # while count <= 5: # count += 1 # print("Loop", count) # # else: # print("循环正常执行完啦") # print("-----out of while loop ------") # 输出 # Loop 1 # Loop 2 # Loop 3 # Loop 4 # Loop 5 # Loop 6 # 循环正常执行完啦 # -----out of while loop ------ # 如果执行过程中被break啦,就不会执行else的语句啦 # count = 0 # while count <= 5: # count += 1 # if count == 3: break # print("Loop", count) # # else: # print("循环正常执行完啦") # print("-----out of while loop ------") # # 输出 # # Loop 1 # Loop 2 # -----out of while loop ------ #优化! count = 0 name = 'loky' password = '123' while count < 3: in_name = input('请输入用户账号:') in_pwd = input('请输入用户密码:') if name == in_name and password == in_pwd: print('登陆成功!欢迎尊贵的{}会员'.format(name)) while True: cmd = input('请输入‘执行命令:') if cmd == 'q': break else: print('系统正在执行命令{x}'.format(x=cmd)) break else: print('输入密码错误') count += 1 else: print('用户输入错误三次,系统自动退出!')
python入门之流程控制while循环
最新推荐文章于 2024-09-26 19:15:00 发布
本文详细讲解了Python中的while循环结构,包括基本用法、死循环、循环控制(break和continue)、嵌套循环以及while...else语句的应用。通过登陆验证的实例,展示了如何在循环中使用条件判断实现用户输入的检查。
摘要由CSDN通过智能技术生成