Look up a number by name.
What to build
A dictionary built from input, and one lookup.
Requirements
- Read
nlines ofNAME NUMBER. - Read one more line with a name.
- Print that person's number, or
Not found.
Input
The first line has n. Each of the next n lines has a name and a number. The last line has the name to find.
Output
One line.
Constraints
n is from 1 to 100. If a name appears twice, the later number replaces the earlier one.
Example
Input:
2
Rahim 017
Nadia 018
Nadia
Output:
018
Why it works
Assigning to an existing key replaces its value, which is what a phone book update should do.
Examples
Example 1 · Input
2 Rahim 017 Nadia 018 Nadia
Expected output
018
Example 2 · Input
1 Rahim 017 Karim
Expected output
Not found
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Build the dictionary with a loop over the first n lines.
Hint 2
Use get with a default, or test with in.
Solution
Show one correct solution (try it yourself first)
n = int(input())
book = {}
for _ in range(n):
name, number = input().split()
book[name] = number
wanted = input()
print(book.get(wanted, "Not found"))