您的位置:首页 > 编程语言 > Python开发

python-基础知识之while循环

2018-01-25 00:28 429 查看
一、while 语法

while 条件:
# 循环体

# 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
# 如果条件为假,那么循环体不执行,循环终止

示例:

#打印0-10
count=0
while count <= 10:
print('loop',count)
count+=1

#打印0-10之间的偶数
count=0
while count <= 10:
if count%2 == 0:
print('loop',count)
count+=1

#打印0-10之间的奇数
count=0
while count <= 10:
if count%2 == 1:
print('loop',count)
count+=1
二、死循环

import time
num=0
while True:
print('count',num)
time.sleep(1)
num+=1 三、练习
#练习,要求如下:
1 循环验证用户输入的用户名与密码
2 认证通过后,运行用户重复执行命令
3 当用户输入命令为quit时,则退出整个程序

#实现一:
name='egon'
password='123'

while True:
inp_name=input('用户名: ')
inp_pwd=input('密码: ')
if inp_name == name and inp_pwd == password:
while True:
cmd=input('>>: ')
if not cmd:continue
if cmd == 'quit':
break
print('run <%s>' %cmd)
else:
print('用户名或密码错误')
continue
break

#实现二:使用tag
name='egon'
password='123'

tag=True
while tag:
inp_name=input('用户名: ')
inp_pwd=input('密码: ')
if inp_name == name and inp_pwd == password:
while tag:
cmd=input('>>: ')
if not cmd:continue
if cmd == 'quit':
tag=False
continue
print('run <%s>' %cmd)
else:
print('用户名或密码错误')四、break 和 continue
#break用于退出本层循环
while True:
print "123" #打印(1次) 123
break #跳出while循环
print "456" #不执行

#continue用于退出本次循环,继续下一次循环
while True:
print "123" #打印(N次) 123
continue #循环第(2,3,4,5...)次。但不执行continue之后的代码
print "456" #不执行五、while...else...组合使用
#与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句,while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句
count = 0
while count <= 5 :
count += 1
print("Loop",count)

else:
print("循环正常执行完啦")
print("-----out of while loop ------")
输出
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
循环正常执行完啦
-----out of while loop ------

#如果执行过程中被break啦,就不会执行else的语句啦
count = 0
while count <= 5 :
count += 1
if count == 3:break
print("Loop",count)

else:
print("循环正常执行完啦")
print("-----out of while loop ------")
输出

Loop 1
Loop 2
-----out of while loop ------六、while 练习题
#1. 使用while循环输出1 2 3 4 5 6 8 9 10
#2. 求1-100的所有数的和
#3. 输出 1-100 内的所有奇数
#4. 输出 1-100 内的所有偶数
#5. 求1-2+3-4+5 ... 99的所有数的和
#6. 用户登陆(三次机会重试)
#7:猜年龄游戏
要求:
允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
#8:猜年龄游戏升级版
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出
练习题答案:

#题一
count=1
while count <= 10:
if count == 7:
count+=1
continue
print(count)
count+=1

count=1
while count <= 10:
if count != 7:
print(count)
count+=1

#题目二
res=0
count=1
while count <= 100:
res+=count
count+=1
print(res)

#题目三
count=1
while count <= 100:
if count%2 != 0:
print(count)
count+=1

#题目四
count=1
while count <= 100:
if count%2 == 0:
print(count)
count+=1

#题目五
res=0
count=1
while count <= 5:
if count%2 == 0:
res-=count
else:
res+=count
count+=1
print(res)

#题目六
count=0
while count < 3:
name=input('请输入用户名:')
password=input('请输入密码:')
if name == 'egon' and password == '123':
print('login success')
break
else:
print('用户名或者密码错误')
count+=1

#题目七
age_of_oldboy=73

count=0
while count < 3:
guess=int(input('>>: '))
if guess == age_of_oldboy:
print('you got it')
break
count+=1

#题目八
age_of_oldboy=73

count=0
while True:
if count == 3:
choice=input('继续(Y/N?)>>: ')
if choice == 'Y' or choice == 'y':
count=0
else:
break

guess=int(input('>>: '))
if guess == age_of_oldboy:
print('you got it')
break
count+=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: