Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中读取user 2值并找到最高GPA?_Python_Loops_While Loop_Int_Gpa - Fatal编程技术网

如何在python中读取user 2值并找到最高GPA?

如何在python中读取user 2值并找到最高GPA?,python,loops,while-loop,int,gpa,Python,Loops,While Loop,Int,Gpa,如何编写一个python代码,在一行中读取用户的学名和GPA,但如果用户输入一个类似(off)的单词,程序将停止。。我想使用while循环,计算并打印最高的GPA和学生姓名 2个值=第一个int。。二线。 like=GPA。。。名字 data = [] while True: inp = [i for i in input("Please enter your name followed by your GPA: ").strip().split()] if (len(inp)=

如何编写一个python代码,在一行中读取用户的学名和GPA,但如果用户输入一个类似(off)的单词,程序将停止。。我想使用while循环,计算并打印最高的GPA和学生姓名

2个值=第一个int。。二线。 like=GPA。。。名字

data = []
while True:
    inp = [i for i in input("Please enter your name followed by your GPA: ").strip().split()]
    if (len(inp)==1 or inp[0] == 'off'): break
    data.append({'Name':(' '.join([str(i) for i in inp[:-1]])) , 'GPA':float(inp[-1])})

# print(data)

不确定您想对结果做什么,也不确定如何存储结果,但这应该可以让您从需要的内容开始

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

请告诉我们您到目前为止尝试了什么,以及您遇到的错误。谢谢。我不能用split??是的,为什么不用?:)姓名、成绩(“输入您的姓名和成绩:”)。分级时拆分(“,”)=“停止”:如果等级>=90,等级=80,等级=70,等级=0,等级为0,这就是问题所在。。编写一个程序,从用户那里读取几行输入。每行包括一名学生的姓名和他/她的GPA(实数)。该程序应计算并打印最高GPA(四舍五入至小数点后2位)以及达到该成绩的学生姓名。
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