Answer to Question #299497 in Python for Naman

Question #299497

A teacher has taken exam of his students. He has stored marks in a dictionary where keys are the student's name and values are student's marks. Now he wants to know the name of students obtained minimum and maximum marks from this dictionary. You need to write a python function Find-min-max(A) which takes dictionary as input and returns the name of student pair having minimum and maximum marks in the class. If input dictionary is empty return "Invalid Input".

1
Expert's answer
2022-02-18T07:50:27-0500
def find_min_max(D: dict):
	min_name = None
	max_name = None
	for name in D:
		if min_name is None:
			min_name = name
		if max_name is None:
			max_name = name
		if D[name] > D[max_name]:
			max_name = name
		if D[name] < D[min_name]:
			min_name = name
	if min_name is None:
		return "Invalid Input"
	return f"min mark: {min_name}\nmax mark: {max_name}"


input_dict = {
			'Name 1': 50, 'Name 2': 60,
			'Name 3': 40, 'Name 4': 30,
			'Name 5': 100, 'Name 6': 90,
			'Name 7': 80}


print(find_min_max(input_dict))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS