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".
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))
Comments
Leave a comment