Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Don't excute my code
#1
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
Reply


Messages In This Thread
Don't excute my code - by RavCOder - Sep-30-2019, 10:31 AM
RE: Don't excute my code - by buran - Sep-30-2019, 10:38 AM
RE: Don't excute my code - by RavCOder - Sep-30-2019, 12:49 PM
RE: Don't excute my code - by jefsummers - Sep-30-2019, 02:13 PM
RE: Don't excute my code - by ichabod801 - Oct-01-2019, 12:34 PM
RE: Don't excute my code - by RavCOder - Oct-01-2019, 12:48 PM
RE: Don't excute my code - by ichabod801 - Oct-01-2019, 01:21 PM
RE: Don't excute my code - by jefsummers - Oct-01-2019, 01:48 PM
RE: Don't excute my code - by buran - Oct-01-2019, 02:10 PM
RE: Don't excute my code - by ichabod801 - Oct-01-2019, 02:34 PM
RE: Don't excute my code - by RavCOder - Oct-01-2019, 02:40 PM
RE: Don't excute my code - by RavCOder - Oct-01-2019, 03:45 PM
RE: Don't excute my code - by ichabod801 - Oct-01-2019, 03:51 PM
RE: Don't excute my code - by RavCOder - Oct-01-2019, 03:56 PM
RE: Don't excute my code - by jefsummers - Oct-01-2019, 04:22 PM
RE: Don't excute my code - by RavCOder - Oct-02-2019, 10:21 AM
Problem with dictionary - by RavCOder - Oct-01-2019, 07:58 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020