--------------------------------------------------------
count = 0
while count < 20:
print(count)
count = count + 1
---------------------------------------------------------
带else的while循环,条件不成立的时候执行else,只执行一次。
count = 0
while count < 10:
print(count)
count = count + 1
else:
print("else")
---------------------------------------------------------
continue解释:
遇到continue后while循环会立刻跳到while的开始处执行,即continue是终止while的当前次的循环;
count的用法举例:(输出除3以外的1到10的所有数字)
count = 0
while count < 10:
if count == 3:
count = count + 1
continue
print(count)
count = count + 1
---------------------------------------------------------
break解释:
遇到break立刻跳出while循环,即break是终止当前while的所有循环;
break、while、if综合运用举例(用户登录账号和密码,只允许用户尝试3次,第4次输入锁定账号):
# -*- conding: utf-8 -*-
StrUserName = "0"
StrPassword = "0"
StrTest = "hello"
StrTest2 = "123456"
A = 0
while StrUserName != StrTest or StrPassword != StrTest2:
A = A + 1
if A == 4:
print("账号锁定")
break
else:
StrUserName = input("请输入用户名:")
StrPassword = input("请输入密码:")
if StrUserName == StrTest and StrPassword == StrTest2:
print("欢迎登录")
else:
print("账号或密码错误")