Question

Write a PYTHON program for the following: A dictionary courses lists summer school classes a student...

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.

Homework Answers

Answer #1

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).

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT