Python练习:设计函数,实现计算随机月份的平均访客量。
时间: 2024-03-05 20:50:00 浏览: 21
可以设计如下的函数来计算随机月份的平均访客量:
```python
import random
def avg_visitor_count():
visitor_counts = {
'January': 1000,
'February': 1200,
'March': 1300,
'April': 1400,
'May': 1500,
'June': 1700,
'July': 1800,
'August': 1900,
'September': 1600,
'October': 1400,
'November': 1200,
'December': 1100
}
months = list(visitor_counts.keys())
random_month = random.choice(months)
return visitor_counts[random_month]
print(avg_visitor_count())
```
这个函数首先定义了一个字典 `visitor_counts` 来存储每个月份的访客量。然后,它创建了一个列表 `months` 来存储所有的月份。接着,它使用 `random.choice()` 函数从 `months` 列表中随机选择一个月份,并返回该月份的访客量。
你可以多次调用 `avg_visitor_count()` 函数来获取不同月份的平均访客量。