0

I want to know If there is anyway when someone enters, let's say '070718604545'. It Looks it up and if it's there it prints others in the list, Example below

Patt = [
                {'Phone': "0718604545", 'Name': "Tom", 'Age': '2007'}
                {'Phone': "0718123567", 'Name': "Katy", 'Age': '1998'}
                {'Phone': "0718604578", 'Name': "BillyW", 'Age': '1970'}
                 ....
                 ....
                 ....
                 ....
                {'Phone': "0714565778", 'Name': "Sony", 'Age': '1973'}
            ]

He will enter '0718604545' as exmaple.

x = input("Enter Phone")
Search for x in Patt[Phone]:
   name = Patt[Name] where Phone = x
   print(name)

So the answer should be Tom.

Thanks,

3 Answers3

1

You can get the number by iterating over the list patt and then acess each phone key in patt's items and use the == operator to compare with the phone number you are loooking for. The function below does the job.

def answer(search):
    for data in patt: # iterate over each item in patt
        if data["Phone"] == search: # compare the current value if it is the searched one
            return data["Name"] # return the name in the dictionary when the number is found
    return None # if it is not found, return None
print(answer('0718604545'))
leodsc
  • 111
  • 4
0

Below should work. For every item, check if 'phone' key has value matching x. IF yes, then return the value of 'name' key.

x = input("Enter Phone")
for item in Patt:
  if item["Phone"] == x:
    print(item["Name"])
AnkurSaxena
  • 825
  • 7
  • 11
0

while preparing answer, I did not notice that one correct answer was already posted and accepted. Though, posting my version of the answer below with added feature of continuous questioning and an ability to exit from the program.

Patt = [
    {'Phone': "0718604545", 'Name': "Tom", 'Age': '2007'},
    {'Phone': "0718123567", 'Name': "Katy", 'Age': '1998'},
    {'Phone': "0718604578", 'Name': "BillyW", 'Age': '1970'},
    {'Phone': "0714565778", 'Name': "Sony", 'Age': '1973'}
]

print(Patt)


def search_phone_records(user_phone):
    for record in Patt:  # Iterate over all the phone records in the dictionary
        if user_phone == record['Phone']:  # stop if found phone number in the dictionary
            return record['Name']  # Return user's name from the phone record
    return None


while True:
    user_input = input("Enter Phone or press 'x' to exit: ")

    if user_input in ('x', 'X'):
        print("Have a nice day!!! Thank you using our service!!!")
        break  # End the programme

    # Search for the phone number
    user_name = search_phone_records(user_input)

    #print("[{0}]".format(user_name))
    if type(user_name) == type(None):  # Phone number is not found
        print("Oops!!! Entered phone number ({0}) is not found in the dictionary!!!".format(user_input))
    else:  # Phone number is found
        print("Entered phone number ({0}) is found in the dictionary!!!".format(user_input))
        print("It is {1}'s phone number.".format(user_input, user_name))

Another solution using dictionary comprehension:

Patt = [
    {'Phone': "0718604545", 'Name': "Tom", 'Age': '2007'},
    {'Phone': "0718123567", 'Name': "Katy", 'Age': '1998'},
    {'Phone': "0718604578", 'Name': "BillyW", 'Age': '1970'},
    {'Phone': "0714565778", 'Name': "Sony", 'Age': '1973'}
]

print(Patt)


def search_phone_records_using_dictionary_comprehension(user_phone):
    return {'Name': record['Name'] for record in Patt if user_phone == record['Phone']}


while True:
    user_input = input("Enter Phone or press 'x' to exit: ")

    if user_input in ('x', 'X'):
        print("Have a nice day!!! Thank you using our service!!!")
        break  # End the programme

    result = search_phone_records_using_dictionary_comprehension(user_input)
    print("result = {0}".format(result))

    if len(result) == 0:   # Phone number is not found
        print("Oops!!! Entered phone number ({0}) is not found in the dictionary!!!".format(user_input))
    else:  # Phone number is found
        print("Entered phone number ({0}) is found in the dictionary!!!".format(user_input))
        print("It is {1}'s phone number.".format(user_input, result['Name']))
Panchdev Singh
  • 121
  • 1
  • 5