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 lessonThis 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
- Create a Dictionary and Access a ValueBeginner
- Add and Modify Dictionary EntriesBeginner
- Safe Access with get()Beginner
- Loop Through a DictionaryBeginner
- Predict the Output: Duplicate KeysBeginner
- Build a Word CounterIntermediate
- Access Nested Dictionary DataIntermediate
All Python exercises · 50 practice problems · Data Structures quiz