Python bootcamp · Lab 38

Dictionary lookup

mediumDictionaries12 minLesson: Dictionaries

Read the question, write Python on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

The first line is n. Then n lines of 'name score'. Then one query name.

Print that person's score, or missing if the name is not in the map.

Input. n, then n lines of name and integer, then one query name.

Output. The score, or the word missing.

Examples

Example 1
Input
3
alice 90
bob 80
carol 70
bob
Output
80
Example 2
Input
2
a 1
b 2
z
Output
missing
Hint
  1. scores[name] = int(score) as you read.
  2. dict.get(query, 'missing') handles the miss in one call.
Show correct code

Peek only after you have tried. You can still Check your own version.

n = int(input())
scores = {}
for _ in range(n):
    name, score = input().split()
    scores[name] = score
query = input().strip()
print(scores.get(query, 'missing'))
main.pyPython · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.