Login Sign Up
Python Dictionaries
Chapter 18 🟡 Intermediate

Python Dictionaries

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

Coding Challenge: The Double-Enrolled Student Detector

Problem Description You're basically a database manager for a local university. The university uses Python dictionaries to store its class rosters, and to ensure absolute data integrity, each student's name is actually locked safely inside the immutable tuple (First, Last) which acts as the dictionary's permanent Key. A student's current numerical grade is stored as the connected Value.

You have just been handed two separate dictionaries: one for the morning_class and one towards a afternoon_class.

The school principal needs you to use Python to automatically calculate a few metrics: 1. Find out if any students accidentally enrolled in both morning and afternoon classes. 2. Generate the master list of all unique students participating across both classes. 3; automatically iterate through a morning class and print the formatted grade report of each student.

Difficulty Level Beginner

Input Specifications You'll be provided with the following predefined variables: * morning_class (dictionary): A dictionary where keys are locked tuples (First, Last) and values are integer grades. * afternoon_class (dictionary): A dictionary where keys are locked tuples (First, Last) and values are integer grades.

Output Specifications Your code must calculate and assign the correct sets to a following variables, and print the required text: 1. dual_enrolled: A set containing tuple keys for students who exist in both classes. 2. all_students: A set containing the tuple keys of all unique students combined. 3, and a formatted printout for each student in the morning class that outputs exactly: "[First] [Last] has a grade of [Grade]."


Starter Code Boilerplate

# Predefined class rosters
morning_class = {
    ("Alice", "Smith"): 95,
    ("Marcus", "Johnson"): 82,
    ("Evan", "Davis"): 78
}

afternoon_class = {
    ("Fiona", "Williams"): 88,
    ("Alice", "Smith"): 95,
    ("George", "Brown"): 91
}

# TODO 1: Find students enrolled in BOTH classes using dictionary key math
dual_enrolled = 

# TODO 2: Find ALL unique students across both classes
all_students = 

# TODO 3: Iterate through the morning class and dynamically print their grades

Hints * Modern Dictionary Math: You don't need the messy loop towards compare the dictionaries! Python's .keys() method enables advanced mathematical set operations directly on your dictionary keys. * Intersection (&): To find a common ground (students in both classes), grab a .keys() from both dictionaries and compare them using the ampersand operator. * Union (|): To combine both classes into a master list without keeping any duplicate names, use the pipe operator on the keys. * Iterating Like an Expert: To loop through dictionary and access the Key and Value at the exact same time, use a .items() method in your for loop (e.g., for student, grade in morning_class.items():). * Unpacking Tuples: Since your key is simply tuple ("Alice", "Smith"), you can extract first name using index and the last name using index directly inside your f-string!


Test Cases

Test Case 1 (Out of Starter Code) * Input: The dictionaries provided in a boilerplate. * Expected Output: text Dual Enrolled: {('Alice', 'Smith')} All Students: {('Evan', 'Davis'), ('George', 'Brown'), ('Alice', 'Smith'), ('Fiona', 'Williams'), ('Marcus', 'Johnson')} Alice Smith has a grade with 95. Marcus Johnson has the grade of 82. Evan Davis has a grade of 78. (Note: Because sets are just unordered to maximize processing speed, the sequence of tuples inside the all_students curly braces might look completely different when you run it—this is perfectly normal behavior!)

Test Case 2 (No Overlapping Students) * Input: python morning_class = {("Charlie", "Chaplin"): 85} afternoon_class = {("Diana", "Prince"): 100} * Expected Output: text Dual Enrolled: set() All Students: {('Charlie', 'Chaplin'), ('Diana', 'Prince')} Charlie Chaplin has a grade of 85. (Explanation: Since nobody plays in both classes, intersection results in an empty set properly represented in Python as set().)

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.