码迷,mamicode.com
首页 > 其他好文 > 详细

流程控制之while循环

时间:2018-09-14 22:54:26      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:tag   过程   代码   bre   应用   pwd   支付   inpu   put   

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循环

标签:tag   过程   代码   bre   应用   pwd   支付   inpu   put   

原文地址:https://www.cnblogs.com/xiamenghan/p/9648988.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!