day 06 流程控制之while/for循环

一、流程控制之while循环

1、为何要用循环?

可以用循环语句减少重复代码

2、如何使用while循环?

1)条件循环

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

#练习:
# 1)打印0-10
count = 0
while count <= 10:
	print(count)
	count += 1

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

# 3)打印0-10之间的奇数
count = 0
while count <= 10:
	if count % 2 == 1:
		print(count)
	count += 1
	

2)死循环

import time
num = 0
while True:
	print('count',num)
	time.sleep(1)  # 时间间隔为1
	num += 1

3)循环嵌套与tag

tag = True
while tag:
	......
	while tag:
		......
		while tag:
			......
			while tag:
				tag = False

# 练习,要求如下:
"""
1 循环验证用户输入的用户名与密码
2 认证通过后,运行用户重复执行命令
3 当用户输入命令为quit时,则退出整个程序 
"""
# 实现一:
name = 'egon'
password = '123'

while True:
	inp_name = input{'Username:)
 	inp_pwd = input('Password:)
 	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('erroe')
		continue
	break

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

tag = True
while tag:
	inp_name = input('Username:')
	inp_pwd = input('Password:')
	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 Trueprint'123'
	continue
	print('456')

强调1:不要在continue之后编写同级别的代码
count = 0
while count < 6: # 5 < 6
    if count == 3
    or count == 4:
        count += 1
        continue
        # count+=1 # 不要写在这里
    print(count)
    count += 1


强调2: 如果不想执行本次循环之后的代码,可以用continue,但是如果本次循环本来就没有要继续运行的后续代码了,就没必要加continue了
db_name = "egon"
db_pwd = "123"

while True:
    inp_name = input("请输入您的用户名: ")
    inp_pwd = input("请输入您的密码: ")

    if inp_name == db_name and inp_pwd == db_pwd:
        print("用户登录成功")
        break
    else:
        print("用户账号或密码错误")
        # continue

5、while + else

#与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句,while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

count = 0
while count < 6:
    print(count)
    if count == 3:
        break
    count+=1
else:
    print('会在while循环正常死亡之后运行')


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、结束流程

# tag的方式结束循环
db_name = "egon"
db_pwd = "123"

tag = True
while tag:
    inp_name = input("请输入您的用户名: ")
    inp_pwd = input("请输入您的密码: ")

    if inp_name == db_name and inp_pwd == db_pwd:
        print("用户登录成功")

        while tag:
            print("""
            0 退出
            1 取款
            2 提现
            3 转账
            """)
            cmd=input("请输入您的命令编号:")
            if cmd == "0":
                tag = False
            elif cmd == "1":
                print("正在取款")
            elif cmd == "2":
                print("正在提现")
            elif cmd == "3":
                print("正在转账")
            else:
                print("不知道的指令,请重新输入")


    else:
        print("用户账号或密码错误")

7、while循环嵌套

db_name = "egon"
db_pwd = "123"

while True:
    inp_name = input("请输入您的用户名: ")
    inp_pwd = input("请输入您的密码: ")

    if inp_name == db_name and inp_pwd == db_pwd:
        print("用户登录成功")

        while True:
            print("""
            0 退出
            1 取款
            2 提现
            3 转账
            """)
            cmd=input("请输入您的命令编号:")
            if cmd == "0":
                break
            elif cmd == "1":
                print("正在取款")
            elif cmd == "2":
                print("正在提现")
            elif cmd == "3":
                print("正在转账")
            else:
                print("不知道的指令,请重新输入")
        break
    else:
        print("用户账号或密码错误")

二、流程控制之for循环

"""
1、for循环主要用于循环取值,例如列表、字典、字符串
2、for循环循环的次数取决于值的个数
    while循环循环的次数取决条件什么时候变为False或者什么时候执行break

"""
一:基本使用

l = [1111, 222, 333, 444, 555]

i = 0
while i < len(l):
    print(l[i])
    i += 1

for x in l:
    print(x)


d = {"k1": 111, "k2": 2222, "k3": 33333}
for k in d:
    print(k,d[k])


msg="hello world"
for x in msg:
    print(x)


l = [["aaa", 1111], ["bbb", 2222], ["ccc", 3333]]
for x, y in l:  # x,y=["aaa",1111]
    print(x, y)


二:for + break
for x in [111,222,333,4444,555]:
    if x == 333:
        break
    print(x)


三:for + continue
for x in [111,222,333,4444,555]:
    if x == 333:
        continue
    print(x)

四:for + else
for x in [111,222,333,4444,555]:
    if x == 333:
        break
    print(x)
else:
    print('=====>')


"""
?????????????

把多个多个正确账号密码存起来

要求用户输入账号
要求用户输入密码


循环:从存放多个正确账号密码的地方取出一组账号和密码
    判断 输入账号 等于 取出的账号 并且 输入的密码 等于 取出的密码:
        告诉用户认证成功
        break
else:
    print("告诉用户输入的账号密码错误")
"""


猜你喜欢

转载自blog.csdn.net/weixin_48283789/article/details/107235476