🐸

The Stock Analyzer

Python 基础python-basics-36-the-stock-analyzer
奖励: 100 XP
|

趋势分析师

欢迎来到华尔街。

在数字森林里,你数过金币。在 现实世界 (Real World) 中,数字代表着随时间变化的价值——比如股价、气温或网站流量。

问题:你有一堆数字,但原始数字不会讲故事。 目标:提炼 洞察 (Insights)。趋势如何?最高点在哪里?

魔法 vs 现实

在这个实验室里,你变身为一名 数据分析师 (Data Analyst)

你的工作不仅仅是存储数据,而是要对数据提问,并使用 Python 的内置数学函数找到答案。

工具箱

Python 用三个咒语瞬间完成统计:

prices = [10, 20, 30]

# 1. sum() - 求和
total = sum(prices)  # 60

# 2. len() - 计数
count = len(prices)  # 3

# 3. max() - 找最大值
highest = max(prices)  # 30

# 平均值 = 总和 / 数量
average = total / count  # 20.0

你的任务

你正在追踪 "HoppyCorp" 过去 7 天的股价。

stock_prices = [120, 122, 115, 130, 128, 142, 135]

1
平均值

计算本周的 平均股价。(提示:使用 sumlen)。将其存储在变量 avg_price 中。

2
最高点

使用 max() 找到本周的 最高股价。将其存储在变量 max_price 中。

3
报告

打印摘要:"Average: [数值], Max: [数值]"。

参考答案
点击展开
参考答案:

只需三行代码,你就可以总结数百万个数据点。

stock_prices = [120, 122, 115, 130, 128, 142, 135]

# 1. 计算平均值
total = sum(stock_prices)
count = len(stock_prices)
avg_price = total / count

# 2. 找到最高点
max_price = max(stock_prices)

# 3. 报告
print("平均股价:", avg_price)
print("最高股价:", max_price)
Loading...
终端输出
Terminal
Ready to run...