标签:错误 lse pass 代码 pwd 结束 print bre 直接
一.结束循环的两种方式
1. 修改条件
tag=True
while tag:
print(1)
print(2)
print(3)
tag=False
print(4)
2.while + break 直接结束本次循环
while True:
while True:
while True:
break
break
break
案列一:特点是在输错三次后还可以打印 提示输入次数过多
name=‘deng‘
pwd=‘123‘
count=0
while True:
name_inp=input(‘please input your name:‘)
pwd_inp=input(‘please input your password:‘)
if name_inp == name and pwd_inp ==pwd:
print(‘login successful‘)
break
else:
print(‘user or password error‘)
count+=1
if count ==3:
print(‘too many error‘)
break
案列二:特点是在输错三次后直接结束循环
name=‘deng‘
pwd=‘123‘
count=0
while count < 3:
name_inp = input(‘please input your name:‘)
pwd_inp=input(‘please input your password:‘)
if name_inp == name and pwd_inp == pwd:
print(‘login successsful‘)
break
else:
print(‘user or password error‘)
count+=1
二. while + continue
continue 代表结束本次循环,直接进入下一次循环
案列一:0到10之间所有的数字,除了5,其余的分别打印出来
n=0
while n < 10:
if n == 5:
n+=1
continue
print(n)
n+=1
不合理示范1
while True:
print(1)
print(2)
print(3)
continue
不合理示范2
while True:
print(1)
print(2)
if 1 == 2:
pass
else:
print(‘xxxx‘)
continue
三.while嵌套
案列一
name=‘deng‘
pwd=‘123‘
count=0
while True:
name_inp=input(‘please input your name:‘)
pwd_inp=input(‘please input your password:‘)
if name_inp == name and pwd_inp == pwd:
print(‘login successful‘)
while True:
print(‘‘‘
0 退出
1 取款
2 转账
3 查询
‘‘‘)
cmd = input(‘请输入指令编号:‘)
if cmd == ‘0‘:
break
elif cmd == ‘1‘:
print(‘取款‘)
elif cmd == ‘2‘:
print(‘转账‘)
elif cmd == ‘3‘:
print(‘查询‘)
else:
print(‘输入的指令错误,请重新输入!!!‘)
break
else:
print(‘user or password error‘)
count+=1
if count == 3:
print(‘输错次数过多!!!‘)
break
案例二:
name=‘egon‘
pwd=‘123‘
count=0
tag=True
while tag:
inp_name=input(‘please input your name: ‘)
inp_pwd=input(‘please input your password: ‘)
if inp_name == name and inp_pwd == pwd:
print(‘login successful‘)
while tag:
print("""
0 退出
1 取款
2 转账
3 查询
""")
cmd = input(‘请输入指令编号>>>: ‘) # cmd=‘0‘
if cmd == ‘0‘:
tag=False
elif cmd == ‘1‘:
print(‘取款...‘)
elif cmd == ‘2‘:
print(‘转账...‘)
elif cmd == ‘3‘:
print(‘查询...‘)
else:
print("输入错误指令,请重新输入")
else:
print(‘user or passwor error‘)
count+=1 #count=3
if count == 3:
print(‘too many tries.....‘)
break
三:while+else
else:如果while循环没有被break打断过,即正常运行完毕后才会执行else的子代码块
n=0
while True:
# if n == 3:
# break
print(n)
n+=1
else:
print(‘run.....‘)
n=0
while n <= 3:
print(n)
n+=1
else:
print(‘run.....‘)
标签:错误 lse pass 代码 pwd 结束 print bre 直接
原文地址:https://www.cnblogs.com/dengyanchuan/p/10209159.html