流程控制之 while 循环

1 为何要用循环
age_of_oldboy = 48

guess = int(input(“>>:”))

if guess > age_of_oldboy :
print(“猜的太大了,往小里试试…”)

elif guess < age_of_oldboy :
print(“猜的太小了,往大里试试…”)

else:
print(“恭喜你,猜对了…”)

#第 2 次
guess = int(input(“>>:”))

if guess > age_of_oldboy :
print(“猜的太大了,往小里试试…”)

elif guess < age_of_oldboy :
print(“猜的太小了,往大里试试…”)

else:
print(“恭喜你,猜对了…”)

#第 3 次
guess = int(input(“>>:”))

if guess > age_of_oldboy :
print(“猜的太大了,往小里试试…”)

elif guess < age_of_oldboy :
print(“猜的太小了,往大里试试…”)

else:
print(“恭喜你,猜对了…”)

#即使是小白的你,也觉得的太 low 了是不是,以后要修改功能还得修改 3 次,因此记住,写重复的代码是程序员最不耻的行为。
那么如何做到不用写重复代码又能让程序重复一段代码多次呢? 循环语句就派上用场啦
2 条件循环: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
3 死循环

import time
num=0
while True:
print(‘count’,num)
time.sleep(1)
num+=1
4 循环嵌套与 tag

tag=True

  while tag:

    ……

    while tag:

      ……..

      while tag:

        tag=False
#练习,要求如下:
循环验证用户输入的用户名与密码
认证通过后,运行用户重复执行命令
当用户输入命令为 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(‘用户名或密码错误’)
4 break 与 continue

#break 用于退出本层循环
while True:
print “123”
break
print “456”

#continue 用于退出本次循环,继续下一次循环
while True:
print “123”
continue
print “456”
5 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 ——
6 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