Search code examples
python-3.xparallel-arrays

Searching Names and phonenumbers


mine is homework question in response to the previous Question i posted on this site:

i redid the code to the following:

    import re
    people = ["Karen", "Peter", "Joan", "Joe", "Carmen", "Nancy", "Kevin"]

    phonenumbers = ["201-222-2222", "201-555-1212", "201-967-1490", 201-333-3333",'201-725-3444", "201-555-1222", "201-444-4656"]

name = raw_input("Enter person's name:")

found = false
for i in range(0, len(people)):

   value = people[i]
   m = ("(" + name + ".*)",value)

   if m:
       found = True
       print (people[i], phonenumber[i])
   else:
       print ("No matching name was found.")

My question is how do i tell the program to check if Karen's phone number is the 201-222-2222? And Yes this is a homework assignment. I changed the names and the phone numbers in my acutal program.

When i run this program and type any character all the names and phone number show up that's where i'm having difficutly...


Solution

  • EDITED: Question isn't clear to me.

    The following code my help.

    1.) First it ask for name then check if it exist in the people list.
    2.) Then if it exist it saves it in variable called abc.
    3.) After loop is finished it prints the abc which is the name you entered and that person phone number.

    import re
    people = ["Karen", "Peter", "Joan", "Joe", "Carmen", "Nancy", "Kevin"]
    
    phonenumbers = ["201-222-2222", "201-555-1212", "201-967-1490", "201-333-3333","201-725-3444", "201-555-1222", "201-444-4656"]
    
    name = input("Enter person's name:")
    abc = "" # Will store the name and phone number
    found = False 
    for i in range(0, len(people)):
       if people[i].lower() == name.lower(): #checks if input name match + use method to lower all char
              abc = people[i]+" Has the number "+phonenumbers[i]
       value = people[i]
       m = ("(" + name + ".*)",value)
       if m:
           found = True
           print (people[i], phonenumbers[i]) # missing letter "s"
       else:
           print ("No matching name was found.")
    
    print("\n"+abc)
    

    Result

    enter image description here