流程控制之while循环

Posted xiamenghan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了流程控制之while循环相关的知识,希望对你有一定的参考价值。

1. 什么是循环

        循环指的是一个重复做某件事的过程

 

2. 为何要有循环

        为了让计算机能够像人一样重复做某件事

3. 如何用循环

 

# while循环的语法:while循环又称为条件循环,循环的次数取决于条件

 

while 条件

    子代码1

    子代码2

    子代码3

 

print(‘strat....‘)

while True:

    name=input(‘please your name :‘)

    pwd=input(‘please your password:‘)

    if name == ‘summer‘ and pwd == ‘123‘:

        print(‘login successful‘)

    else:

        print(‘user or password err‘)

print(‘end....‘)

 

如何结束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+continue:continue代表结束本次循环,直接进入下一次
count=1
while count < 6:
if count == 4:
count+=1
continue # 只能在cotinue同一级别之前加代码
print(count)
count+=1


while True:
print(‘11111‘)
print(‘22222‘)
print(‘333‘)
continue # 不应该将continue作为循环体最后一步执行的代码


while+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循环的嵌套
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.....‘)
 





























































































































































































以上是关于流程控制之while循环的主要内容,如果未能解决你的问题,请参考以下文章

流程控制之判断,while循环,for循环

流程控制之while循环

Python学习————流程控制之while循环

流程控制之if判断+while循环+for循环

PHP流程控制之do...while循环的区别

『Python基础』第6节:流程控制之while循环