python循环语句怎么用 ?_python循环控制语句的应用操作

79 阅读1分钟

while True: print("hello, you")


为了让程序运行到一定阶段退出循环体,需要改变条件,当条件改变到不满足的状态时,就可以退出循环结构了:



times = 0while times < 1000:

print(f"hello you {times}") times += 1


## **for 循环**


for 循环是一种更加常用的循环结构,主要作用**遍历**一组数据达到循环执行的效果。这组数据通常是字符串,列表,元素,字典等可迭代的数据。



my_string = 'hello you' for letter in my_string: print(letter)


letter 是一个临时变量,表示每一轮循环从 my\_string 中取出来的元素,第一轮循环是 `h`, 第二轮循环是 `e` 。临时变量在退出循环结构之后会失效。



for letter in my_string:

print(letter) # YES

print(letter) # NO


**遍历列表**



xyj = ['悟空', '八戒', '白龙马'] for xyj in xyj: print(xyj)


遍历字符串、列表、元组等数据时,可以使用 enumerate 函数同时获取索引和值,经常可以用到。



xyj = ['悟空', '八戒', '白龙马']for index,

item in enumerate(xyj):

print(item)


**遍历字典**


遍历字典默认是获取 key



user = {"name": "悟空", "age": "18"}

for item in user:

print(item)


同时获取 key 和 value 是更常用的做法:  
  



for key, item in user.items():

print(key, item)


**range**


range 的作用是生成一个类似于列表的数据,range(6) 生成类似于 [0,1,2,3,4,5] 的数据。当你需要对某段代码循环运行指定次数,但是又没有现成的数据可以遍历时,可以用 range



for item in range(10000):print(item)


range() 的参数类似于切片的写法,当只有一个参数时,表示结束索引号,当有两个参数时,表示开始和结束的索引号,当有3个参数时,增加步长。



start, end

for item in range(3,8): print(item)

start, end, step

for item in range(3,8,2): print(item)


#### 循环的嵌套


之前我们了解到, for 循环作用是对一组数据中的不同元素执行相同的操作(代码),如果想对不同的元素进行差异化操作,可以使用 for 循环嵌套 if 的组合。  
  



xyj = ['悟空', '八戒', '白龙马']

for xyj in xyj:if xyj == '悟空':

print("大师兄")else:

print("没有你要找的人")


对元素分组:



users = [ {"name": 'yyz', "age": 18}, {"name": '悟空', "age":16}, {"name": 'v', "age": 19}, {"name": 'w', "age": 20}, ]

adult = [] kids = []

for user in users:

user = {"name": 'yyz', "age": 18}

if user['age'] >= 18: adult.append(user) else: kids.append(user)

print(adult) print(kids)


#### breakwhilefor 的循环体中,都可以使用 break 关键字终止整个循环体的运行。尤其是在和 if 的搭配使用中,当满足某个条件时,就终止整个循环结构。



while True: username = input("输入用户名")

paword = input("输入密码")

if username == 'admin' and paword == '123456':

print('login')

break


#### continue


continue 则可以跳过本轮循环,进入下一轮循环。他也常常和 if 搭配使用:



songs = ['传奇','', '礼物', '故乡', '']

for song in songs:

if not song: print("下一曲")

continueprint(f"正在播放:{song}")


#### 循环的自动化测试实际使用


自动化测试场景:表示多个测试数据


1、写一个程序,存储一个测试数据



username = input("请输入用户名:")

pass = input("请输入密码:")

age = input("请输入年龄:")

user = dict()user.update

(username=username, pass=pass, age=age)


2、写一个程序,可以存储多个测试数据



users = list()users.append(user)print(users)


3、添加多个用例,运行多个用例