循坏结构
(一)循环结构
(1)什么是循环结构
- 循环结构是一种程序控制结构,用于反复执行一组语句,直到满足某个条件为止。
- 循环结构使得程序可以更有效地重复执行某段代码,节省了编写重复代码的工作。
(2)循环结构的作用
- 循环结构的主要作用是重复执行一组语句,直到满足某个条件。
- 这种重复执行的过程可以是固定次数的,也可以是根据条件动态确定的。
- 循环结构使得程序可以更灵活、高效地处理需要重复执行的任务。
(3)while循环
(1)语法
- while是循环的关键字
- 条件时循环的条件,条件为真,循坏体就一直执行
- 循环体时需要重复执行的代码块
while condition:
#循坏体
(2)使用
count=1
while count<5:
print(count)
count+=1
#执行结果:
#1
#2
#3
#4
(3)案例:
(4)for循环
(1)语法
- for是循环关键字
variable是循环变量,它会在每次循环中取sequence中的一个值。sequence是一个序列,可以是列表、元组、字符串等。
for variable in sequence:
#循环体
(2)使用
list = [1, 2, 3, 4, 5]
for i in list:
print(i)
#执行结果:
# 1
# 2
# 3
# 4
# 5
(5)退出循环break
(1)语法
while condition:
# 循环体
if some_condition:
break # 退出循环
(2)使用
count=1
while count<5:
print(count)
count+=1
if count==3:
print(count)#3
break#退出循环
#执行结果:
#1
#2
#3
(1)登录案例(while循环----break结束循环)
-
用户登录有三次机会,如果输入正确就直接结束循环
count=0 name='syh' pwd='123' while count<3: username=input('请输入你的用户名:') password=input('请输入你的密码:') if username==name and password==pwd: print('登录成功') break#输入正确的用户名密码后。直接结束循环 else: print('登录失败') count+=1
(6)退出循环continue
(1)语法
while condition:
# 循环体
if some_condition:
continue # 跳过当前循环,继续下一次循环
(2)使用
count=0
while count<5:
count += 1
if count==3:#当count=3时,跳出本次循环,继续循环
continue
print(count)
#执行结果:跳过count=3
#1
#2
#4
#5
(7)无限循环(死循环)
- 有时候,我们需要程序在满足某个条件时一直执行,这就需要用到无限循环。
- 最简单的无限循环可以通过
while语句实现,条件永远为真。
while True:
print("hello world")
- 这段代码会一直输出 "hello world",因为
while True:的条件永远为真,所以循环不会停止。 - 在实际编程中,我们可能会在无限循环中加入某个条件来实现根据需要退出循环的逻辑。
(8)标志位
(1)语法
- flag = True # 标志位
flag = True
while flag:
#循坏体
if some_condition:
flag = False # 修改标志位,退出循环
(2)使用
flag=True
while flag:
username_input=input('请输入hello:')
if username_input == 'hello':
print('hello!!!!')
flag=False#修改标志位,退出循环
else:
print('no hello ')
#执行结果:
#请输入hello:hello
#hello!!!!
(9)循环分支 while +else
- while循环后面可以跟上else
- 当while循环执行完毕后没有停止,就会继续执行else
count=0
while count<5:
print('hello')
count+=1
else:
print('stop')
#执行结果
#hello
#hello
#hello
#hello
#hello
#stop
补充 range关键字
(1)遍历数字序列
- 语法
- range(stop)从0开始到stop结束
- range(start, stop)从start开始到stop结束
- range(start, stop, step)从start开始到stop结束step是步长
for i in range(stop): #从0开始到stop结束
# 循环体
for i in range(start, stop):#从start开始到stop结束
# 循环体
for i in range(start, stop, step):#从start开始到stop结束step是步长
# 循环体
- 使用
#遍历数字序列
for i in range(3):
print(i)
# 0
# 1
# 2
#指定区间
for i in range(1, 5):
print(i)
# 1
# 2
# 3
#指定步长
for i in range(1, 10, 3):
print(i)
# 1
# 4
# 7
(2)range+len遍历序列
(1)原理
list = [1, 2, 3, 4, 5, 6]
for i in range(len(list)):
#循环体
(2)使用
list = [1, 2, 3, 4, 5, 6]
for i in range(len(list)):
print(f'{i},{list[i]}')
#执行结果:
# 0,1
# 1,2
# 2,3
# 3,4
# 4,5
# 5,6
(3)range创建
(1)原理
numbers = list(range(start, stop, step))
(2)使用
numbers = list(range(0, 10))
print(numbers)
# 输出:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
(二)循环分支嵌套
- 循环结构和分支结构可以嵌套在一起,形成复杂的程序逻辑。
while True:
score = input("请输入您的成绩:")
if score.isdigit():
score = int(score)
if score > 100 or score < 0:
print("您的输入有误!")
elif score > 90:
print("成绩优秀")
elif score > 70:
print("成绩良好")
elif score > 60:
print("成绩及格")
else:
print("成绩不及格")
else:
print("请输入一个数字")
#执行结果
#请输入您的成绩:66
#成绩及格

浙公网安备 33010602011771号