Print the Student Name and the Score of Student in Python3

How to display name and grade for a student in a dictiionary who has the highest grade

What your code is doing right now is iterating through each key-value pair of the dictionary (where the key is the student's name and the value is the list of the student's grades), and then calculating the average for the student. Then, the way you are using max right now, it is trying to find the max of a single student's average. This is why you are receiving the error, because max expects either an iterable or multiple parameters, and a float (which is the value produced by sum(v) / float(len(v))) is not an iterable. You should instead compute all of the averages first, and then find the max value in the dictionary of averages:

diction = {
'Andrew': [56, 79, 90, 22, 50],
'Colin': [88, 62, 68, 75, 78],
'Alan': [95, 88, 92, 85, 85],
'Mary': [76, 88, 85, 82, 90],
'Tricia': [99, 92, 95, 89, 99]
}

def averageGrades(diction):
avgDict = {}
for k, v in diction.items():
avgDict[k] = sum(v) / len(v)

return max(avgDict.items(), key=lambda i: i[1]) # find the pair which has the highest value

print(averageGrades(diction)) # ('Tricia', 94.8)

Sidenote, in Python 3, using / does normal division (as opposed to integer division) by default, so casting len(v) to a float is unnecessary.

Alternatively, if you don't need to create the avgDict variable, you can just determine the max directly without the intermediate variable:

def averageGrades(diction):
return max([(k, sum(v) / len(v)) for k, v in diction.items()], key=lambda i: i[1])

print(averageGrades(diction)) # ('Tricia', 94.8)

How could I print the highest score and name of student grades using python? [closed]

There are a few things you could do to improve it. You find the min score but don't use it, so I'm not sure if you need it. If you do need it, you could add it to your return statement. Here's a suggested way to do it that should be easy to follow:

students = [["steve", 9.0], ["ben", 9.5], ["Caroline", 9.6], ["Tim", 9.1]]


def best(students):
highest_grade_name = None
lowest_grade_name = None
my_max_score = -float("inf")
my_min_score = float("inf")
for name, score in students:
if score > my_max_score:
my_max_score = score
highest_grade_name = name
if score < my_min_score:
my_min_score = score
lowest_grade_name = name
return my_max_score, highest_grade_name


best_name, best_score = best(students)
print(f"The best student is {best_name} with a score of {best_score}")

Program Allows User To Input 5 Student Names And Marks To Print A Report (loop)

Here is some working code:

n = 5

students=[]
for num in range(n):
x=raw_input("Enter name of students: ")
students.append(x)

marks=[]
for num in range(n):
y=raw_input("Enter marks of students:")
marks.append(y)

report = raw_input("Do you want to print class report?: ")
if report == 'yes':
for i,j in zip(students, marks):
print(i,':', j)

Explanation

Several issues resolved:

  • Indentation is crucial in Python.
  • Use list.append on variables that are actually defined.
  • Use a loop with zip to cycle through pairs of [student, mark] data.
  • Use raw_input instead of input in Python 2.x.

I want to be able to search a name I input and get a score

If you want to have one score for each student, I don't think you want to have a list at all -- instead have a dictionary that maps each student name to their score.

students = {
input('Enter student name: '): float(input('Enter score: '))
for _ in range(int(input('Enter the amount of students: ')))
}

which produces something like:

Enter the amount of students: 2
Enter student name: Sam
Enter score: 1
Enter student name: Bob
Enter score: 5
>>> students
{'Sam': 1.0, 'Bob': 5.0}
>>> students['Sam']
1.0
>>> print(students.get(input('Enter a student name: '), 'No such student.'))
Enter a student name: Sam
1.0

I'm having trouble understanding this code

I have added comments in the code for better understanding, hope this helps.

from __future__ import print_function
# Create an empty dict
score_list = {}
# Take a number input and run this loop that many times
for _ in range(input()):
name = raw_input()
score = float(raw_input())
# if value of score is an existing key in dict, add name to the value of that key
if score in score_list:
score_list[score].append(name)
else:
# Else create a key with score value and initiate the value with a list
score_list[score] = [name]
new_list = []
for i in score_list:
new_list.append([i, score_list[i]])
new_list.sort()
result = new_list[1][1]
result.sort()
print (*result, sep = "\n")

Creating and finding maximum score in a list of students?

You should consider of using dict instead of list. then in dict {'id':'score'} you can sort and print score by id. using three lists you won;t be able to connect element of one list with element of other lists.

in this case read python manual about dictionaries. They are really good written manuals.

about dict you can use i.e. dict={'id':['name','score']} so key will be student id, and value of this key will be list of name and score. it's easy to navigate on it.



Related Topics



Leave a reply



Submit