python - 如何从 python 中的用户 2 值读取并找到最高 GPA?
问题描述
如何编写 python 代码以在一行中从用户读取学生姓名和 GPA,但是如果用户输入类似 (off) 的单词,程序将停止.. 我想使用 while 循环并计算并打印最高 GPA 和学生姓名?
2 个值 = 第一个 int .. 第二个字符串。喜欢= GPA ...名称
解决方案
不确定您想对结果做什么或如何存储它们,但这应该让您开始使用您需要的东西。
from collections import defaultdict
def get_mean_grades():
students = defaultdict(list)
while True:
data = input('Enter your GPA followed by your name: ')
if data.upper() == 'STOP':
break
else:
gpa, name = data.split(' ', 1)
students[name].append(float(gpa))
print()
for student, grades in students.items():
average = sum(grades) / len(grades)
print(f"{student} has an average grade of {average}")
Enter your GPA followed by your name: 4.3 Tom Morris
Enter your GPA followed by your name: 2.2 Fred York
Enter your GPA followed by your name: 4.8 Tom Morris
Enter your GPA followed by your name: 3.3 Fred York
Enter your GPA followed by your name: STOP
Tom Morris has an average grade of 4.55
Fred York has an average grade of 2.75
推荐阅读
- tensorflow - 什么错误“ ConcatOp :输入的尺寸应该匹配:
- mysql - Group_Concat 与连接
- c# - Raycast 未检测到自定义 Unity UI 按钮脚本
- python - Python Google Search Webscraping Query Error: builtins.TypeError: search() got an unexpected keyword argument 'tld'
- javascript - 在 setState 中 React 推送 Api 响应
- flutter - 通过 MethodChannel 传递 Isolate/ControlPort
- android - 如何使用 videoPlayer 滚动 recyclerview 视频帧
- spring-kafka - Spring Cloud Stream Kafka 批处理 - 手动提交整个批处理
- regex - 如何确定 RNA 中的所有基因序列,其中 G 和 T 的组合至少重复一次
- python - Pygame Tower 发射多发子弹如何修复?