六、流程控制之while循环
1、什么是循环?
循环指的是一个重复做某件事的过程
2、为何要有循环?
为了让计算机像人一样能循环去做某件事
3、如何用循环
while循环的语法:while循环又称为条件循环。循环的次数取决于条件
while语法1:
while 条件: 子代码1 子代码2 子代码3 print('start....') while True: name=input('please your name>>: ') pwd=input('please your password>>: ') if name == 'egon' and pwd == '123': print('login successful') else: print('user or password err') print('end...')
while语法2:如何结束while循环
方式一:操作while循环的条件让其结束 print('start....') tag=True while tag: name=input('please your name>>: ') pwd=input('please your password>>: ') if name == 'egon' and pwd == '123': print('login successful') tag=False else: print('user or password err') print('end...') 方式二: break强行终止本层循环 count=1 while count < 6: print(count) count+=1 count=1 while True: if count > 5: break print(count) count+=1
print('start....')
while True:
name=input('please your name>>: ')
pwd=input('please your password>>: ')
if name == 'egon' and pwd == '123':
print('login successful')
break
else:
print('user or password err')
print('end...')
输错三次则退出 方式一: print('start....') count=0 while count <= 2: #count=3 name=input('please your name>>: ') pwd=input('please your password>>: ') if name == 'egon' and pwd == '123': print('login successful') break else: print('user or password err') count+=1 print('end...') 方式二 print('start....') count=0 while True: if count == 3: print('输错的次数过多傻叉') break name=input('please your name>>: ') pwd=input('please your password>>: ') if name == 'egon' and pwd == '123': print('login successful') break else: print('user or password err') count+=1 print('end...')
while语法3:continue
count=1 while count < 6: if count == 4: count+=1 continue # 只能在cotinue同一级别之前加代码 print(count) count+=1 while True: print('11111') print('22222') print('33333') continue # 不应该将continue作为循环体最后一步执行的代码
while语法4:else
count=1 while count < 6: if count == 4: break print(count) count+=1 else: print('会在while循环没有被break终止的情况下执行') 输错三次则退出之while+else的应用 print('start....') count=0 while count <= 2: #count=3 name=input('please your name>>: ') pwd=input('please your password>>: ') if name == 'egon' and pwd == '123': print('login successful') break else: print('user or password err') count+=1 else: print('输错的次数过多') print('end...')
while语法5:循环嵌套
name_of_db='egon' pwd_of_db='123' print('start....') count=0 while count <= 2: #count=3 name=input('please your name>>: ') pwd=input('please your password>>: ') if name == name_of_db and pwd == pwd_of_db: print('login successful') while True: print(""" 1 浏览商品 2 添加购物车 3 支付 4 退出 """) choice=input('请输入你的操作: ') #choice='1' if choice == '1': print('开始浏览商品....') elif choice == '2': print('正在添加购物车....') elif choice == '3': print('正在支付....') elif choice == '4': break break else: print('user or password err') count+=1 else: print('输错的次数过多') print('end...') # tag控制所有while循环 name_of_db='egon' pwd_of_db='123' tag=True print('start....') count=0 while tag: if count == 3: print('尝试次数过多') break name=input('please your name>>: ') pwd=input('please your password>>: ') if name == name_of_db and pwd == pwd_of_db: print('login successful') while tag: print(""" 1 浏览商品 2 添加购物车 3 支付 4 退出 """) choice=input('请输入你的操作: ') #choice='1' if choice == '1': print('开始浏览商品....') elif choice == '2': print('正在添加购物车....') elif choice == '3': print('正在支付....') elif choice == '4': tag=False else: print('user or password err') count+=1 print('end...')