Как прочитать из пользователя 2 значения в python и найти самый высокий средний балл? - PullRequest
0 голосов
/ 26 апреля 2020

Как мне написать python код для чтения от пользователя Имени студента и ГПД в одну строку, но если пользователь вводит слово типа (выкл), программа останавливается ... Я хочу использовать, пока l oop и Вычислите и напечатайте наивысший средний балл и имя студента?

2 значения = First int .. Second String. как = GPA ... имя

Ответы [ 2 ]

0 голосов
/ 26 апреля 2020

Не уверен, что вы хотите сделать с результатами или как вы хотите их сохранить, но это должно помочь вам начать с тем, что вам нужно.

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
0 голосов
/ 26 апреля 2020
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)
Please enter your name followed by your GPA: Kuldeep Singh 2.1
Please enter your name followed by your GPA: Kuldeep 3.1
Please enter your name followed by your GPA: Peter Smith 4.0
Please enter your name followed by your GPA: off
[{'GPA': 2.1, 'Name': 'Kuldeep Singh'},
 {'GPA': 3.1, 'Name': 'Kuldeep'},
 {'GPA': 4.0, 'Name': 'Peter Smith'}]

enter image description here

...