Python Forum

Full Version: Don't excute my code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Task:
Given n names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.
Input Format:
The first line contains an integer,n , denoting the number of entries in the phone book.
Each of the n subsequent lines describes an entry in the form of 2 space-separated values on a single line. The first value is a friend's name, and the second value is an 8-digit phone number.

After the n lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a name to look up, and you must continue reading lines until there is no more input.

Output format:
On a new line for each query, print Not found if the name has no corresponding entry in the phone book; otherwise, print the full name and phoneNumber in the format name=phoneNumber.

This is my code that I improve:
def list_address_book(n, address_book):
    n = int(input())
    address_book = dict()
    user_input = input()
    key , value = user_input.split()
    address_book [key] = value
    for key , value in address_book :
        if key == "":
            print("Not found")
        else:
         print (address_book[key]  [value])

    

if __name__ == "__main__":
    list_address_book(n,address_book)
Error:
Traceback (most recent call last): File "Solution.py", line 19, in <module> list_address_book(n,address_book) NameError: name 'n' is not defined
If there is someone who explains to me and tells me where I went wrong it would be welcome.

Thanks and regards,
RavCoder
n is not defined (i.e. no value assign to variable n before you use it on line 16) anywhere....I don't see how the traceback can be more clear

Next error message would be NameError about address_book
Hi,
thanks I didn't understand the first time, but how do I to call n and address phone if these parameters are user's input ? because I can't to put values to test and I don't know how write a dictionary as parameters.
Suggest - take the two tasks as separate. First function, create_address_book() would be called with no parameters. This routine would return the address book. Then you call the list_address_book(address_book) function which reads the rest of the file and creates the desired list. Your main would then look like:
if __name__ == "__main__":
    list_address_book(create_address_book())
and your two functions would do the two tasks in the assignment.

One challenge to this approach is that the file will be used in both functions, so you will need a way to handle that. Pass it as an additional parameter would be one approach. Globals are usually frowned upon but would be another.
Hi,
I tried this solution for this challenge: Given n names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.
The result should be this:

sam=99912222
Not found
harry=12299933
and the input format should be this:
3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
This is my code that I developed:
def list_address_book():
    n = int(input())
    address_book = dict()
    user_input = input()
    key , value = user_input.split()
    address_book [key] = value
    for key , value in address_book :
        if key == "":
            print("Not found")
        else:
         print (address_book[key]  [value])

    

if __name__ == "__main__":
    list_address_book()
Error:
Traceback (most recent call last): File "Solution.py", line 19, in <module> list_address_book() File "Solution.py", line 10, in list_address_book for key , value in address_book : ValueError: too many values to unpack (expected 2)
I don't know if I put global variables or I was wrong some condition. Huh
Thanks and regards,
RavCoder
To loop over key, value pairs of a dictionary, loop over dict_name.items().
I don't understand.Can you explain me this dict_name.items because I never see this in Python.
Thanks,
RavCoder
There are three ways to loop over a dictionary. Say we have this dictionary:

knights = {'Lancelot': 'Blue', 'Robin': "I don't know that", 'Galahad': 'Blue, no green', 'Arthur': 'What do you mean?'}
If we do a plain loop over the dictionary (for name in knights:) we get the keys: Lancelot; Robin; Galahad; and Arthur. We can also loop over the values (for answer in knights.values():) and get Blue; I don't know that; Blue, no green; and What do you mean? We can also loop over both with items (for name, answer in knights.values():) and get the pairs (Lancelot, Blue); (Robin, I don't know that); and so on.
In the last code snippet above I believe it should be in knights.items().

Think of a dictionary as a collection of items. Each item contains a key and a value. You can access by keys, values, or by whole items which give you both the key and the value.
One thing that I think OP has wrong is the input format:

(Sep-30-2019, 10:31 AM)RavCOder Wrote: [ -> ]Input Format:
The first line contains an integer,n , denoting the number of entries in the phone book.
Each of the n subsequent lines describes an entry in the form of 2 space-separated values on a single line. The first value is a friend's name, and the second value is an 8-digit phone number.

After the n lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a name to look up, and you must continue reading lines until there is no more input.


Reading this I think it's either a file or a multi-line string that is in the above format, e.g.
my_input = '''3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry'''
As they have in this post. But in their code they take input from user.

They need to (i) parse it in a phone book (i.e. a dict) and list of names for query, (ii) then they need to query the phone book with the list of names. At least this is how I understand the Task as described:

(Sep-30-2019, 10:31 AM)RavCOder Wrote: [ -> ]Task:
Given n names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.
Pages: 1 2