代码改变世界

Python-流程控制之循环

2018-11-20 16:18  huangdong丶  阅读(177)  评论(0编辑  收藏  举报
# 1. while循环:条件循环
# I:基本语法
# while 条件:
# 代码1
# 代码2
# 代码3
# ...

# 示范
# name_of_bk='huangdong'
# paw_of_bk='123'
#
# tag=True
# while tag:
# inp_name=input('your name>>:')
# inp_pwd=input('your password>>:')
# if inp_name == name_of_bk and inp_pwd == paw_of_bk:
# print('login successful')
# tag=False
# else:
# print('username or password error')
#
# print('emmmmmm')

# II: while+break:break代表结束本层循环
# 示范:
# while True:
# print(1)
# break
# print(2)
# print(3)

# name_of_bk='huangdong'
# paw_of_bk='123'
#
# while True:
# inp_name=input('your name>>:')
# inp_pwd=input('your password>>:')
# if inp_name == name_of_bk and inp_pwd == paw_of_bk:
# print('login successful')
# break
# else:
# print('username or password error')
#
# print('emmmmmm')

# III: while + continue: continue代表结束本次循环,直接进入下一次
# 示范
# count=1
# while count <6:
# if count == 3:
# count+=1
# continue
# print(count)
# count+1

# 输错三次退出
# name_of_bk='huangdong'
# paw_of_bk='123'
#
# count=0
# while True:
# if count == 3:
# print('输错次数过多..')
# break
# inp_name=input('your name>>:')
# inp_pwd=input('your password>>:')
# if inp_name == name_of_bk and inp_pwd == paw_of_bk:
# print('login successful')
# break
# else:
# print('username or password error')
# count+=1 #count=3 输错3次
#
#
# print('emmmmmm')

# IV:while + else
# count=0
# while True:
# if count == 10:
# break
# print(count)
# count+=1
# else:
# print('else的子代码块只有在while循坏没有被break打断的情况下才会执行')

# count=0
# while count <= 10:
# print(count)
# count+=1
#
# else:
# print('else的子代码块只有在while循坏没有被break打断的情况下才会执行')


name_of_bk='huangdong'
pwd_of_bk='123'
count=0
tag=True
while tag:
if count == 3:
print('输错次数过多...')
break
inp_name=input('your name>>:')
inp_paw=input('your password>>:')
if inp_name == name_of_bk and inp_paw == inp_paw:
print('login password')
while tag:
print('''
0 退出
1 购物
2 支付
3 查看购物车
''')
cmd=input('>>:')
if cmd == '0':
tag=False
continue
if cmd == '1':
print('购物...')
elif cmd =='2':
print('支付..')
elif cmd == '3':
print('购物车')
else:
print('输入错误的指令')

else:
print('username or password error')