Python 循环结构例题

Python 循环结构例题

Python 循环结构例题

1. 什么是循环结构

循环结构是程序中用于重复执行特定代码块的一种结构。Python 提供了多种循环结构,包括 for 循环和 while 循环。

1.1 for 循环

for 循环语法格式如下:

for <variable> in <sequence>:
    <code block>
Python

其中,<variable> 是一个变量,用于存储当前循环的元素值;<sequence> 可以是一个列表、字符串、元组等可迭代对象;<code block> 是需要重复执行的代码块。

for 循环的执行过程如下:
1. 遍历 <sequence> 中的每一个元素。
2. 将当前元素赋值给 <variable>
3. 执行 <code block>
4. 重复步骤1~3,直到遍历完所有元素。

下面是一个简单的示例代码:

fruits = ['apple', 'banana', 'orange', 'grape']

for fruit in fruits:
    print(fruit)
Python

运行结果:

apple
banana
orange
grape

1.2 while 循环

while 循环语法格式如下:

while <condition>:
    <code block>
Python

其中,<condition> 是一个条件表达式,返回布尔值。只有当 <condition>True 时,才会执行 <code block>

while 循环的执行过程如下:
1. 检查 <condition> 是否为 True
2. 如果是,则执行 <code block>
3. 重复步骤1~2,直到 <condition>False

下面是一个简单的示例代码:

count = 0

while count < 5:
    print(count)
    count += 1
Python

运行结果:

0
1
2
3
4

2. 循环中的控制流程

在循环结构中,我们通常使用一些控制流程语句来控制循环的执行流程。下面介绍几个常用的控制流程语句。

2.1 break 语句

break 语句用于终止循环,并跳出循环体。当程序执行到 break 语句时,循环会立即终止。

下面是一个示例代码:

fruits = ['apple', 'banana', 'orange', 'grape']

for fruit in fruits:
    if fruit == 'orange':
        break
    print(fruit)
Python

运行结果:

apple
banana

2.2 continue 语句

continue 语句用于跳过当前循环的剩余代码,直接进行下一次循环。当程序执行到 continue 语句时,循环会跳过该次循环的剩余部分,并开始下一次循环。

下面是一个示例代码:

fruits = ['apple', 'banana', 'orange', 'grape']

for fruit in fruits:
    if fruit == 'orange':
        continue
    print(fruit)
Python

运行结果:

apple
banana
grape

2.3 else 语句

else 语句用于在循环正常结束后执行,即在循环条件为 False 时执行。如果循环被 break 语句终止,则不会执行 else 语句。

下面是一个示例代码:

fruits = ['apple', 'banana', 'orange', 'grape']

for fruit in fruits:
    if fruit == 'mango':
        print('Found mango')
        break
else:
    print('Mango not found')
Python

运行结果:

Mango not found

3. 循环结构的应用实例

循环结构在实际应用中非常常见。下面通过几个实例来演示循环结构的应用。

3.1 计算阶乘

阶乘是一个常见的数学问题,指将一个非负整数的所有小于等于它的数相乘。例如,5 的阶乘为 5! = 5 * 4 * 3 * 2 * 1 = 120。

下面是使用 for 循环计算阶乘的示例代码:

n = 5
factorial = 1

for i in range(1, n+1):
    factorial *= i

print(f"The factorial of {n} is {factorial}.")
Python

运行结果:

The factorial of 5 is 120.

3.2 猜数游戏

猜数游戏是一个经典的小游戏,玩家需要在给定的范围内猜一个随机生成的数,直到猜中为止。

下面是一个使用 while 循环编写的猜数游戏的示例代码:

import random

target = random.randint(1, 100)
guess = None
count = 0

while guess != target:
    guess = int(input("Guess the number (between 1 and 100): "))
    count += 1

    if guess < target:
        print("Too low!")
    elif guess > target:
        print("Too high!")
    else:
        print(f"Congratulations! You guessed the number in {count} attempts.")
Python

运行结果:

Guess the number (between 1 and 100): 50
Too low!
Guess the number (between 1 and 100): 75
Too high!
Guess the number (between 1 and 100): 63
Too high!
Guess the number (between 1 and 100): 56
Too low!
Guess the number (between 1 and 100): 60
Too low!
Guess the number (between 1 and 100): 62
Congratulations! You guessed the number in 6 attempts.

4. 总结

本文介绍了 Python 中的循环结构,包括 for 循环和 while 循环的基本用法,以及循环中常用的控制流程语句。同时,还通过阶乘计算和猜数游戏等实例演示了循环结构在实际应用中的使用方法。

循环结构是编程中非常重要的概念,掌握好循环的用法可以帮助我们编写出更高效、更灵活的程序。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册