Write a PYTHON program for the following:
A dictionary courses lists summer school classes a student is taking, along with information about the classes. For example,
courses = { "CS 101" :
{ "full_name" : "Intro to CS with Python", "instructors" : [ "Sally", "Johnson" ],
"tas" : ["Bob", "Joe",..., "Elroy"], "num_homeworks" : 6, "num_exams" : 2 },
"SPAN-SAA" : { "name" : "Beginning Spanish I", ... },
... }
Write a loop that computes the total number of homeworks that the student has in all of their classes. Bonus: write this in one line using the function sum and a list comprehension.
CODE:
OUTPUT:
RAW CODE:
courses = {
"CS 101" : { "full_name" : "Intro to CS with Python", "instructors" : [ "Sally", "Johnson" ],"tas" : ["Bob", "Joe",..., "Elroy"], "num_homeworks" : 6, "num_exams" : 2 },
"SPAN-SAA" : { "full_name" : "Biginning Spanish I", "instructors" : [ "Sally", "Johnson" ],"tas" : ["Bob", "Joe",..., "Elroy"], "num_homeworks" : 4, "num_exams" : 2 }
}
print(sum(dic["num_homeworks"] for dic in courses.values())) # nested command
# first dic will iterate to each value in courses dictionary
# than calculate sum(dic["num_homeworks"]) and print the result
NOTE:
If You have any doubts feel free to comment in comment
section.
DO VOTE(LIKE).
Get Answers For Free
Most questions answered within 1 hours.