while 条件:
代码1
代码2
代码3
count = 0
while count < 5:
print(count)
count += 1
print('-------->')
纯计算无IO的死循环会导致致命的效率问题
while True:
inp_name = input('请输入您的账号:')
inp_pwd = input('请输入您的密码:')
if inp_name == 'zhoushun' and inp_pwd == '123':
print('登录成功!')
break
else:
print('账号或密码错误')
将条件改为false,等到下次循环判断条件时才会生效
tag = True
while tag:
inp_name = input('请输入您的账号:')
inp_pwd = input('请输入您的密码:')
if inp_name == 'zhoushun' and inp_pwd == '123':
print('登录成功!')
tag = False
else:
print('账号或密码错误')
只要运行到break就会立刻终止本层循环
while True:
inp_name = input('请输入您的账号:')
inp_pwd = input('请输入您的密码:')
if inp_name == 'zhoushun' and inp_pwd == '123':
print('登录成功!')
break # 立刻终止本层循环
else:
print('账号或密码错误')
while True:
while True:
break
break
while True:
inp_name = input('请输入您的账号:')
inp_pwd = input('请输入您的密码:')
if inp_name == 'zhoushun' and inp_pwd == '123':
print('登录成功!')
while True:
cmd = input("请输入你要操作的业务编号:")
if cmd =='q':
break
print('业务{x}正在运行'.format(x=cmd))
break # 立刻终止本层循环
else:
print('账号或密码错误')
tag = True
while tag:
inp_name = input('请输入您的账号:')
inp_pwd = input('请输入您的密码:')
if inp_name == 'zhoushun' and inp_pwd == '123':
print('登录成功!')
while tag:
cmd = input("请输入你要操作的业务编号:")
if cmd =='q':
tag = False
print('业务{x}正在运行'.format(x=cmd))
else:
print('账号或密码错误')
continue:结束本次循环,直接进入下一次
强调:在continue之后添加同级代码毫无意义,因为永远无法执行
count = 0
while count < 6:
if count == 4:
count+=1
continue
print(count)
count+=1
针对break:else包含的代码会在while循环结束后,并且while循环是在没有被break打断的情况下正常结束的,才会运行
count = 0
while count < 6:
if count == 4:
count+=1
continue
print(count)
count+=1
else:
print('else包含的代码会在while循环结束后,并且while循环是在没有被break打断的情况下正常结束的,才会运行')
count = 0
tag = True
while tag:
if count == 3:
print('输错次数超过三次')
break
inp_name = input('请输入您的账号:')
inp_pwd = input('请输入您的密码:')
if inp_name == 'zhoushun' and inp_pwd == '123':
print('登录成功!')
while tag:
cmd = input("请输入你要操作的业务编号:")
if cmd =='q':
tag = False
print('业务{x}正在运行'.format(x=cmd))
else:
print('账号或密码错误')
count += 1
tag = True
count = 0
while count < 3:
inp_name = input('请输入您的账号:')
inp_pwd = input('请输入您的密码:')
if inp_name == 'zhoushun' and inp_pwd == '123':
print('登录成功!')
while True:
cmd = input("请输入你要操作的业务编号:")
if cmd =='q':
break
print('业务{x}正在运行'.format(x=cmd))
break
else:
print('账号或密码错误')
count += 1
else:
print('输错三次,退出')