Skip to content

本阶段学习目标 在这个阶段,你将学习Python中的列表、循环和随机数,让程序能够处理更多数据并自动完成重复任务。

新概念解释

  1. 什么是"列表"(List)? 生活比喻:就像一个购物清单,可以存放多个物品。
python
# 创建一个购物清单
shopping_list = ["苹果", "牛奶", "面包", "鸡蛋"]

在代码中:

python
categories = ["房租", "餐饮", "交通", "娱乐", "购物", "医疗", "其他"]
categories 是一个列表,包含了7个支出类别

每个类别用引号包围,表示它们是文字(字符串)

用方括号 [] 把所有的项括起来

用逗号 , 分隔每个项

  1. 什么是"循环"(Loop)? 生活比喻:就像工厂的流水线,对每个产品执行相同的操作。

在Python中,for循环会逐个取出列表中的每个元素:

python
for category in categories:
    # 对每个category执行相同的操作
  1. 什么是"随机数"(Random)? 生活比喻:就像掷骰子,每次结果都不一样。

Python中的random模块可以生成随机数:

random.uniform(50, 200) → 生成50到200之间的随机小数

🔍 代码逐行详细解释

第一部分:导入模块和初始化

python
import random

解释:告诉Python"我要使用随机数功能"。就像你要做饭,先告诉家人"我要用厨房"一样。

python
print("=== 月度支出分析 ===")

解释:显示程序标题。

python
# 创建支出类别列表
categories = ["房租", "餐饮", "交通", "娱乐", "购物", "医疗", "其他"]
expenses = []
解释:

categories → 预定义的支出类别列表

expenses = [] → 创建一个空列表,用来存放用户输入的支出金额

第二部分:使用循环收集数据

python
# 使用循环输入各项支出
for category in categories:
    expense = float(input(f"请输入{category}支出: "))
    expenses.append(expense)

详细解释:

第一次循环:

category = "房租"

显示:请输入房租支出:

用户输入数字,比如 2500

expenses.append(2500) → 把2500添加到expenses列表中

第二次循环:

category = "餐饮"

显示:请输入餐饮支出:

用户输入数字,比如 1500

expenses.append(1500) → 把1500添加到expenses列表中

...如此循环7次,直到所有类别都询问完毕。

重要概念:

f"请输入{category}支出: " → 把变量插入到字符串中

.append() → 列表的"添加"方法,就像往购物车里放商品

第三部分:数据分析和统计

python
# 计算统计信息
total_expenses = sum(expenses)
max_expense = max(expenses)
min_expense = min(expenses)
avg_expense = total_expenses / len(expenses)
详细解释:

sum(expenses) → 计算列表中所有数字的总和

max(expenses) → 找出列表中最大的数字

min(expenses) → 找出列表中最小的数字

len(expenses) → 获取列表的长度(有多少个元素)

total_expenses / len(expenses) → 用总和除以数量得到平均值
python
# 找出支出最高的类别
max_index = expenses.index(max_expense)
max_category = categories[max_index]

详细解释:

expenses.index(max_expense) → 在expenses列表中找出最大值的位置

比如expenses = [2500, 1500, 500, 300, 200, 100, 50],最大值2500在位置0

categories[0] → 取出categories列表中第0个元素("房租")

注意:在编程中,位置从0开始计数,不是从1开始!

第四部分:显示分析结果

python
print("\n=== 支出分析 ===")
print(f"总支出: {total_expenses:.2f} 元")
print(f"最高支出: {max_expense:.2f} 元 ({max_category})")
print(f"最低支出: {min_expense:.2f} 元")
print(f"平均支出: {avg_expense:.2f} 元")

解释:

:.2f → 格式化为保留2位小数

({max_category}) → 在括号中显示支出最高的类别名称

第五部分:预算控制模拟(使用while循环)

python
# 使用while循环模拟月度预算控制
print("\n=== 预算控制模拟 ===")
budget = float(input("设置本月预算: "))
current_spending = 0
days = 0

解释:

获取用户设置的预算金额

current_spending = 0 → 初始化当前花费为0

days = 0 → 初始化天数为0

python
while current_spending < budget and days < 30:
    daily_spend = random.uniform(50, 200)
    current_spending += daily_spend
    days += 1
    print(f"第{days}天: 花费{daily_spend:.2f}元,累计{current_spending:.2f}元")

while循环详细解释:

条件:current_spending < budget and days < 30

意思是:当 当前花费小于预算 并且 天数小于30天时,继续循环

循环体内的操作:

daily_spend = random.uniform(50, 200) → 生成50-200元的随机花费

current_spending += daily_spend → 累计花费增加

days += 1 → 天数增加1

显示当天的花费和累计花费

生活比喻:就像每天记录开销,直到花完预算或月底为止。

python
    if current_spending >= budget:
        print(f"警告!在第{days}天已超出预算!")

解释:在循环过程中,如果发现花费超过预算,立即显示警告。

python
if current_spending < budget:
    print(f"本月未超预算,剩余{budget - current_spending:.2f}元")

解释:循环结束后,如果还有剩余预算,显示剩余金额。

🎯 运行示例 当你运行程序时,会看到这样的对话:

text
=== 月度支出分析 ===
请输入房租支出: 2500
请输入餐饮支出: 1500
请输入交通支出: 500
请输入娱乐支出: 300
请输入购物支出: 200
请输入医疗支出: 100
请输入其他支出: 50

=== 支出分析 ===
总支出: 5150.00 元
最高支出: 2500.00 元 (房租)
最低支出: 50.00 元
平均支出: 735.71 元

=== 预算控制模拟 ===
设置本月预算: 5000
第1天: 花费123.45元,累计123.45元
第2天: 花费187.23元,累计310.68元
第3天: 花费95.67元,累计406.35元
...
第25天: 花费156.89元,累计5123.45元
警告!在第25天已超出预算!

💡 学习要点总结 通过这个程序,你学会了:

新概念: 列表(List) - 存储多个数据的容器

for循环 - 对列表中的每个元素执行相同操作

while循环 - 在条件满足时重复执行代码

随机数 - 使用random模块生成随机数值

列表方法 - .append(), .index(), len(), sum(), max(), min()

编程技巧: 使用循环避免重复代码

数据统计和分析

条件循环控制

格式化输出

🔧 常见问题解答

Q: 为什么列表的位置从0开始,不是从1开始?

A: 这是计算机科学的历史传统,几乎所有编程语言都这样设计。你可以把位置理解为"偏移量"。

Q: random.uniform(50, 200) 生成的数字包含50和200吗?

A: 是的,它生成50到200之间(包含50和200)的随机小数。

Q: 如果我想增加或减少支出类别怎么办?

A: 只需要修改categories列表,比如删除"医疗",或者添加"学习"。

Q: while循环会无限循环吗?

A: 不会,因为我们的条件days < 30确保了最多循环30次。

🚀 下一步学习建议 你已经掌握了循环和列表,这是编程中非常重要的概念!接下来可以:

尝试修改类别列表,加入你自己的支出类别

改变随机数的范围,观察不同的模拟结果

继续学习第三阶段,了解函数和字典的使用

记住:编程学习就像搭积木,每个新概念都是一个新的积木块,积累多了就能搭建复杂的程序!💪

python
# stage2_lists_loops.py
import random

print("=== 月度支出分析 ===")

# 创建支出类别列表
categories = ["房租", "餐饮", "交通", "娱乐", "购物", "医疗", "其他"]
expenses = []

# 使用循环输入各项支出
for category in categories:
    expense = float(input(f"请输入{category}支出: "))
    expenses.append(expense)

# 计算统计信息
total_expenses = sum(expenses)
max_expense = max(expenses)
min_expense = min(expenses)
avg_expense = total_expenses / len(expenses)

# 找出支出最高的类别
max_index = expenses.index(max_expense)
max_category = categories[max_index]

print("\n=== 支出分析 ===")
print(f"总支出: {total_expenses:.2f} 元")
print(f"最高支出: {max_expense:.2f} 元 ({max_category})")
print(f"最低支出: {min_expense:.2f} 元")
print(f"平均支出: {avg_expense:.2f} 元")

# 使用while循环模拟月度预算控制
print("\n=== 预算控制模拟 ===")
budget = float(input("设置本月预算: "))
current_spending = 0
days = 0

while current_spending < budget and days < 30:
    daily_spend = random.uniform(50, 200)
    current_spending += daily_spend
    days += 1
    print(f"第{days}天: 花费{daily_spend:.2f}元,累计{current_spending:.2f}元")
    
    if current_spending >= budget:
        print(f"警告!在第{days}天已超出预算!")

if current_spending < budget:
    print(f"本月未超预算,剩余{budget - current_spending:.2f}元")

Released under the MIT License.