python字典
来源:互联网 发布:mac 用apple id登陆 编辑:程序博客网 时间:2024/06/06 12:49
计算班级学生和班级的平均成绩
字典相关知识
#!/usr/bin/python3 #!/usr/bin/python3 # -*- coding: utf-8 -*-"""# Author: EricRay# Created Time : 2017-11-20 15:17:10# File Name: dic1.py# Description:计算学生平均成绩和班级平均成绩"""#学生信息lloyd = { "name": "Lloyd", "homework": [90.0, 97.0, 75.0, 92.0], "quizzes": [88.0, 40.0, 94.0], "tests": [75.0, 90.0]}alice = { "name": "Alice", "homework": [100.0, 92.0, 98.0, 100.0], "quizzes": [82.0, 83.0, 91.0], "tests": [89.0, 97.0]}tyler = { "name": "Tyler", "homework": [0.0, 87.0, 75.0, 22.0], "quizzes": [0.0, 75.0, 78.0], "tests": [100.0, 100.0]}#计算平均成绩def average(numbers): total = float(sum(numbers))/len(numbers) return total#按比例进行计算def get_average(student): homework = average(student["homework"]) quizzes = average(student["quizzes"]) tests = average(student["tests"]) return 0.1 * homework + 0.3 * quizzes + 0.6 * tests#等级判定def get_letter_grade(score): if score >= 90: return "A" elif score >= 80: return "B" elif score >= 70: return "C" elif score >= 60: return "D" else: return "F"#测试lloydprint (get_letter_grade(get_average(lloyd)))#班级平均成绩计算def get_class_average(class_list): results = [] #用于返回结果 for student in class_list: student_avg = get_average(student) results.append(student_avg) return average(results) #参与计算的学生students = [lloyd, alice, tyler]class_avg = get_class_average(students)#平均分数print (class_avg)#等级判定 print (get_letter_grade(class_avg))
阅读全文
1 0
- Python 字典
- python 字典
- Python:字典
- Python 字典
- python字典
- Python 字典
- python 字典
- python 字典
- Python 字典
- Python字典
- python 字典
- python字典
- python 字典
- python 字典
- python--字典
- python 字典
- python字典
- Python---字典
- IoniC3+cordova 实现全屏
- TableViewCell内含textField的复用
- 最全app上传渠道入口 拿去即用
- 软件测试修炼之道
- StringUtils中 isNotEmpty和isNotBlank
- python字典
- MSCOMM发送UTF8乱码问题解决方法
- Resnet解决了什么问题
- LeetCode第一题
- 设计模式(三)单例模式
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
- lua学习笔记(3)——C API基础和栈
- 小知识点整理
- 前端模块化、组件化开发