Python 学习笔记 语法入门之while循环
Python中有while与for两种循环机制,其中while循环称之为条件循环
一、条件循环
1、循环的语法与基本使用 语法
while使用语法
"""
while 条件: # 条件为真执行以下代码
代码1
代码2
代码3
"""
# 例1
count = 0
# 循环判单,条件count小于3,循环一次加1
print('循环开始')
while count < 3:
print(count)
count += 1
print('循环结束')
2、死循环与效率问题
纯计算无IO(输入输出)的死循环代码是致命的效率问题
错误的循环执行 示例
# 例1 无致命效率问题
while true:
name = input('输入——————')
print(name)
# 例2 致命效率问题 代码
while true:
1+1
3、循环的应用
应用方法
username = 'hyjkk'
passwrod = '123456'
while 1: # 条件为真
inp_name = input('请输入你的账号:')
inp_pwd = input('请输入你的密码:')
if inp_name == username and inp_pwd == passwrod:
print('登录成功')
else:
print('账号或密码错误')
4、退出循环
方式1: 设置条件为False
# 方式1: 设置条件为False,等到下次循环判单条件时才会生效
username = 'hyjkk'
passwrod = '123456'
tag = True
while tag:
inp_name = input('请输入你的账号:')
inp_pwd = input('请输入你的密码:')
if inp_name == username and inp_pwd == passwrod:
print('登录成功')
tag=False #之后的代码还会运行,下次循环判断时才会生效。
else:
print('账号或密码错误')
print('登录结束') #不管上述代码执行是否成功,执行到这里运行完成
方式2:break
# 方式2:break,只要运行到break就会立刻终止本层循环
username = 'hyjkk'
passwrod = '123456'
while True:
inp_name = input('请输入你的账号:')
inp_pwd = input('请输入你的密码:')
if inp_name == username and inp_pwd == passwrod:
print('登录成功')
break #立刻终止本层循环,下面代码不再运行
else:
print('账号或密码错误')
5、while嵌套应用
break 结束嵌套
# break 结束方式
username = 'hyjkk'
passwrod = '123456'
while True:
inp_name = input('请输入你的账号:')
inp_pwd = input('请输入你的密码:')
if inp_name == username and inp_pwd == passwrod:
print('登录成功')
while True:
cmd = input("需要执行的命令")
if cmd == 'exit':
break
print('代码{xxx}正在运行'.format(xxx=cmd))
break #结束本层代码
else:
print('账号或密码错误')
条件达成方式
# 改变条件的方式
username = 'hyjkk'
passwrod = '123456'
tag=True
while tag:
inp_name = input('请输入你的账号:')
inp_pwd = input('请输入你的密码:')
if inp_name == username and inp_pwd == passwrod:
print('登录成功')
while tag:
cmd = input("需要执行的命令")
if cmd == 'exit':
tag = False
else:
print('代码{xxx}正在运行'.format(xxx=cmd))
else:
print('账号或密码错误')
6、while + continue 在continue同级下不能添加任何代码,因为不会执行。
count = 0 # count初始为0
while count < 6: # 条件count必须小于6
if count == 4: # 当 count 等于4时 执行内层代码。continue执行回来后此时count=5不具备 执行内层代码的条件。直接执行到print输出。
count += 1 # 此时 count = 4 +1= 5 如果不添加,将会进入无止境的循环中,count永远等4因为执行不到外层count += 1这行代码
continue # 当count=5后结束本次循环 再回到顶级代码运行一次
print(count) # 此时 输出 0 1 2 3 5 没有4因为被continen拦截了
count += 1 #当 count=5 再 +1 得 6 ,此时不满足再循环条件 循环结束
7、while + else 应用,主要针对break使用
例,else不会运行
# 例,else不会运行
count = 0
while count < 6:
if count == 4:
break
print(count)
count += 1
else:
print('不会运行')
例,else运行
# 例,else运行
count = 0
while count < 6:
if count == 4:
count += 1
continue
print(count)
count += 1
else:
print('else包含的代码会在while循环结束后,并且while循环是在没有被break打断的情况下正常结束的,才会运行')
#
else 案例
username = 'hyjkk'
passwrod = '123456'
count = 0
while count < 3: #3次循环条件
inp_name = input('请输入你的账号:')
inp_pwd = input('请输入你的密码:')
if inp_name == username and inp_pwd == passwrod:
print('登录成功')
while True:
cmd = input("需要执行的命令")
if cmd == 'exit': # 退出整个程序,循环结束
print('程序退出')
break # 结束内部while循环
else:
print('代码{xxx}正在运行'.format(xxx=cmd))
break # 内部循环结束后,运行到这行,结束主循环
else:
print('账号或密码错误')
count += 1
else: # 判断条件3次循环结束后,运行。
print("3次错误,请一小时后再试")
8、while循环应用案例
应用案列 1
# 应用案列 1
username = 'hejun'
passwrod = "123"
count=0
tag=True
while tag:
if count == 3:
print('错误超过3次了')
break
inp_name = input('请输入您账号:')
inp_pwd = input('请输入您密码:')
if inp_name == username and inp_pwd == passwrod:
print('登录成功')
while tag:
cmd=input("运行命令>>:")
if cmd == "q":
tag = False
else:
print('命令{xxx}正在运行'.format(xxx=cmd))
else:
print('账号或密码错误')
count += 1
应用案例2
# 应用案例2
username = 'hejun'
passwrod = "123"
count=0
while count < 3:
inp_name = input('请输入您账号:')
inp_pwd = input('请输入您密码:')
if inp_name == username and inp_pwd == passwrod:
print('登录成功')
while True:
cmd=input("运行命令>>:")
if cmd == "q":
break
else:
print('命令{xxx}正在运行'.format(xxx=cmd))
break
else:
print('账号或密码错误')
count += 1
else:
print('错误超过3次登录结束')

浙公网安备 33010602011771号