Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a program that has two parallel arrays: a String array named people that

ID: 3634608 • Letter: D

Question

Design a program that has two parallel arrays: a String array named people that is initialized with the names of seven of your friends, and a String array named phoneNumbers that is initialized with your friends’ phone numbers. The program should allow the user to enter a person’s name ( or part of a person’s name). It should then search for that person in the people array. If the person is found, it should get that person’s phone number from the phoneNumbers array and display it. If the person is not found in the people array, the program should display a message indicating so.


I need this homework assignment due tomorrow, and im absolutely confused as to how to do this. help?

Explanation / Answer

# create main def main(): # take in name or part of persons name person = raw_input("Who are you looking for? > ") # convert string to all lowercase for easier searching person = person.lower() # run people search with the "person" as the parameters peopleSearch(person) # create module to search the people list def peopleSearch(person): # create list with the names of the people people = ["john", "tom", "buddy", "bob", "sam", "timmy", "ames"] # create list with the phone numbers, indexes are corresponding with the names # people[0] is phoneNumbers[0] etc. phoneNumbers = ["5503942", "9543029", "5438439", "5403922", "8764532", "8659392", "9203940"] Since this is homework, I'll refrain from giving the code outright. You can create a dict that works as a lookup table with the name as the key and the phone number as its value. Creating the lookup table: You can easily convert the parallel arrays into a dict using dict() and zip(). Something along the lines of: lookup = dict(zip(people, phoneNumbers)) To see how that works, have a look at this example: >>> people = ["john", "jacob", "bob"] >>> phoneNumbers = ["5503942", "8659392", "8659392"] >>> zip(people, phoneNumbers) [('john', '5503942'), ('jacob', '8659392'), ('bob', '8659392')] >>> dict(zip(people, phoneNumbers)) {'jacob': '8659392', 'bob': '8659392', 'john': '5503942'} Finding if a person exist: You can quickly figure out if a person (key) exist in the lookup table using: if name in lookup: # ... phone number will be lookup[name]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote