This code saves a number of key and value pair from the user but I want to go further and enter keys to be searched in phone book.Can someone tell me what's wrong with my code
phonebook = {}
n = int(input())
for x in range (n):
name , phoneno = input().split()
phonebook[ name ] = int(phoneno)
for y in phonebook:
name = input().split()
if name in phonebook:
print("Found")
else:
print('Not Found')
phonebook = {}
n = int(input())
for x in range (n):
name , phoneno = input().split()
phonebook[ name ] = int(phoneno)
name = input().split()
out = phonebook.get(name,None)
if out == None:
print('Not found')
else:
print('found')
You don't need a loop to check for a key in a dictionary
In name, phoneno = input().split()
the .split()
needs an argument on what to split it on, so lets say you did thisGuy 69420, if you did .split(' ')
space being the argument name & phoneno
would both be a list, which at index [0]
would be thisGuy
and at index[1]
would be 69420
Hopefully, that helps:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.