Given a dictionary that tell you a person\'s major, write a Python function that
ID: 3879520 • Letter: G
Question
Given a dictionary that tell you a person's major, write a Python function that, given a person's name and a major, returns True if that person is majoring that subject, and False otherwise In : def check_major (name, major, majors_dict): # Your code here. return is majoir In Art History", "Irene": "Engineering""Robert""1 majors1"Josh" "Engineering", "Carol": "Accounting" Douglas check major ("Douglas", "Art History", majors_1) -True {"Josh": "Engineering" "Carol": "Accounting" "Douglas": "Art History", "Irene": "Engineering", "Robert": "1 majors-1 check_major("Josh", "Accounting", majors_1)False In [ ]: = , ,Explanation / Answer
def check_major(name,major,majors_dict): is_major = False if majors_dict[name] == major: #dictionary key,value pair. Here name is key and value is major #majors_dict[name] gives major of name is_major = True #True only if given major matches with person's major in dictionary return is_major def check_major2(name,major,majors_dict): is_major = False if name in majors_dict[major]: #name present in list #majors_dict[major] give list persons under that major is_major = True return is_major # majors_1 = {"Josh":"Engineering","Carol":"Accounting","Douglas":"Art History","Irene":"Engineering"} # print check_major("Douglas","Art History",majors_1) # # majors_2 = {"Engineering":["Josh","Robert","Irene"],"Accounting":["Carol","Christine","Charles"],"Art History":["Douglas"]} # print check_major2("Douglas","Art History",majors_2) # print check_major2("Josh","Accounting",majors_2)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.