请用python完成编写函数avg(a,b,c),它可以返回a,b,c的整数平均值return int(a+b+c)/3,调用avg(a,b,c)函数求每位学生的平均成绩。
时间: 2023-10-22 07:07:46 浏览: 303
好的,下面是编写函数 `avg(a,b,c)` 并调用求平均成绩的Python代码:
```python
# 定义函数 avg,返回 a, b, c 的整数平均值
def avg(a, b, c):
return int(a + b + c) / 3
# 定义一个字典,表示每位学生的成绩
scores = {
'张三': [80, 90, 85],
'李四': [70, 85, 90],
'王五': [90, 85, 95]
}
# 遍历字典,求每位学生的平均成绩
for name, score in scores.items():
avg_score = avg(*score) # 使用 *score 展开列表,传递给函数 avg
print(name, '的平均成绩为:', avg_score)
```
输出结果:
```
张三 的平均成绩为: 85.0
李四 的平均成绩为: 81.66666666666667
王五 的平均成绩为: 90.0
```
希望能够帮助到您!
相关问题
编写函数avg(a,b,c),它可以返回a,b,c的整数平均值return int(a+b+c)/3,调用avg(a,b,c)函数求每位学生的平均成绩。
好的,以下是你需要的 Python 代码:
```python
def avg(a, b, c):
return int(a + b + c) / 3
students = {'01': [67, 88, 45], '02': [97, 68, 85], '03': [97, 98, 95], '04': [67, 48, 45], '05': [82, 58, 75], '06': [96, 49, 65]}
for student_id, scores in students.items():
avg_score = avg(*scores)
print("学生", student_id, "的平均成绩为:", avg_score)
```
输出:
```
学生 01 的平均成绩为: 66.66666666666667
学生 02 的平均成绩为: 83.33333333333333
学生 03 的平均成绩为: 96.66666666666667
学生 04 的平均成绩为: 53.333333333333336
学生 05 的平均成绩为: 71.66666666666667
学生 06 的平均成绩为: 70.0
```
在这里,我们使用了Python中的 `*` 符号来将列表 `scores` 拆解为独立的参数,然后传递给 `avg()` 函数。
用python 编写函数,计算列表中元素的平均值。从键盘输入一个列表,调用该函数。
好的,以下是代码实现:
```python
def calculate_average(lst):
sum = 0
for num in lst:
sum += num
avg = sum / len(lst)
return avg
lst = input("请输入列表,元素之间用空格隔开:").split()
lst = [int(num) for num in lst]
avg = calculate_average(lst)
print("列表元素的平均值为:", avg)
```
运行该程序后,会提示您输入一个列表,例如:
```
请输入列表,元素之间用空格隔开:1 2 3 4 5
```
然后程序会输出该列表元素的平均值:
```
列表元素的平均值为: 3.0
```
注意,这里假设您输入的是整数列表,如果您输入的是浮点数列表,那么函数返回的也是浮点数平均值。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)