n = int(input()) phonebook = {} for i in range(n): name, number = input().split()…
- User
n = int(input()) phonebook = {} for i in range(n): name, number = input().split() phonebook[number] = name m = int(input()) for i in range(m): l_name = input() if l_name in phonebook: print(l_name+'='+phonebook(l_name)) else: print('Not found') input: 3 sam 99912222 tom 11122222 harry 12299933 sam edward harry Traceback (most recent call last): File "/tmp/submission/20240314/10/08/hackerrank-b9fd32c5a8c57f0db1fd64313cf1f82d/code/Solution.py", line 6, in m = int(input()) ^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: 'sam'
- Assistant
The error occurs because the input is not in the expected format after the phonebook entries have been provided. The code expects an integer denoting the number of queries (
m) but instead receives a string ('sam'), which cannot be converted into an integer.This usually happens when the script is provided with input in a single block and does not pause for input between entering the phonebook entries and the queries. The input code needs to correctly distinguish the end of the phonebook entries and the start of the query inputs.
To fix this, make sure the input is being provided in the right order and is well formatted. When running the code in an interactive manner, provide the inputs line by line, as the script waits for them using
input().Also, modify the code to correctly associate phone numbers with names, and to look up the correct phone number for a given name: