【0801 | Day 6】Python基础(四)

Part 13 流程控制之while循环

一、语法

while 条件
    code 1
    code 2
    code 3
    ...
​
while True:
    print('*1'*100)
    print('*2'*100)
# 实现ATM的输入密码重新输入的功能
while True:
    user_db = 'nick'
    pwd_db = '123'
​
    inp_user = input('username: ')
    inp_pwd = input('password: ')
    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
    else:
        print('username or password error')

二、while+break

break 命令终止,结束本层循环,执行下面的代码

while True:
    user_db = 'nick'
    pwd_db = '123'
​
    inp_user = input('username: ')
    inp_pwd = input('password: ')
    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
        break
    else:
        print('username or password error')
​
print('退出了while循环')
username: nick
username: nick
password: 123
login successful
退出了while循环    

三、while+continue

continue命令结束本次循环,继续下一次循环

#第一种
n = 1
while n < 4:
    print(n)
    n += 1
    
#第二种
n = 1
while n < 10:
    if n == 8:
        # n += 1  # 如果注释这一行,则会进入死循环(比如n=8,此时执行n+=1命令,n=9不符合if条件,n值不变化依旧为9一直小于10,也不等于8,导致进入死循环)
        continue
    print(n)
    n += 1

注意:continue不能加在循环体的最后一步执行的代码,因为代码加上去毫无意义

while True:
    if 条件1:
        code1
        code2
        code3
        ...
    else:
        code1
        code2
        code3
        ...
​
    continue

四、while循环的嵌套

与用户进行更复杂的信息交互

# 退出内层循环的while循环嵌套
while True:
    user_db = 'nick'
    pwd_db = '123'
​
    inp_user = input('username: ')
    inp_pwd = input('password: ')
​
    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
​
        while True:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                break
            print(f'{cmd} 功能执行')
    else:
        print('username or password error')
​
print('退出了while循环')
​
​
# 退出双层循环的while循环嵌套
while True:
    user_db = 'nick'
    pwd_db = '123'
​
    inp_user = input('username: ')
    inp_pwd = input('password: ')
​
    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
​
        while True:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                break
            print(f'{cmd} 功能执行')
        break
    else:
        print('username or password error')
​
print('退出了while循环')
​
username: nick
password: 123
login successful
请输入你需要的命令:q
退出了while循环

五、tag控制循环推出

# tag控制循环退出
tag = True
while tag:
    user_db = 'nick'
    pwd_db = '123'
​
    inp_user = input('username: ')
    inp_pwd = input('password: ')
​
    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
​
        while tag:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                tag = False
            print(f'{cmd} 功能执行')
    else:
        print('username or password error')
​
print('退出了while循环')
username: nick
password: 123
login successful
请输入你需要的命令:q
q 功能执行
退出了while循环

六、while+else

while+else:else会在while没有被break时才会执行else中的代码

# while+else
n = 1
while n < 3:
    print(n)
    n += 1
else:
    print('else会在while没有被break时才会执行else中的代码')   
1
2
else会在while没有被break时才会执行else中的代码    

Part 14 流程控制之for循环

一、语法

while:

  1. 会进入死循环(不可控),尽量少用while循环

  2. 世间万物都可以作为循环的对象

for:

  1. 不会进入死循环(可控),尽量使用for循环

  2. 只对容器类数据类型进行循环(可迭代对象)

二、for+break

# for+break
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
    if name == 'jason':
        break
    print(name)
nick

三、for+continue

# for+continue
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
    if name == 'jason':
        continue
    print(name)   
nick
tank
sean

continue和break区别:前者跳过后面循环前面(本次),后者跳过前面执行后面(本层)

四、for循环嵌套

外层循环循环一次,内层循环循环所有的

# for循环嵌套
for i in range(3):
    print(f'-----:{i}')
    for j in range(2):
        print(f'*****:{j}')      
-----:0
*****:0
*****:1
-----:1
*****:0
*****:1
-----:2
*****:0
*****:1        

 

五、for+else

for循环没有break的时候触发else内部代码块

# for+else
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
    print(name)
else:
    print('for循环没有被break中断掉')   
nick
jason
tank
sean
for循环没有break中断掉    

六、for循环实现loading

import time
​
print('Loading', end='')
for i in range(6):
    print(".", end='')
    time.sleep(0.2)   
Loading......    

 

 

原文地址:https://www.cnblogs.com/fxyadela/p/11285151.html