Editor

Fix the Bug: Checking for a Value

Intermediate · Fix the bug · from Python Dictionaries: The Complete Guide with Real Examples

From the lesson: Python Dictionaries: The Complete Guide with Real Examples

Open the contacts app on your phone. You look up a person by name and get their phone number. You don't search by position - you don't say "give me contact number 47." You search by name.

Read the lesson

This code tries to check if the grade 'A' exists in the dictionary, but it's checking the wrong thing. Fix it so it prints:

Someone got an A!
grades = {'Alice': 'A', 'Bob': 'B', 'Charlie': 'C'}
if 'A' in grades:
print('Someone got an A!')
else:
print('No one got an A.')

More Dictionaries exercises

All Python exercises · 50 practice problems · Data Structures quiz