The following problem is a homework assignment (I'll list the problem first then my coding)... Looking forward to hear from you.
Problem: Design a program that has two parallel arrays: a string array named people that is initialized with the names of 7 of your friends, and a string array named phone numbers that is initialized with your friends phone numbers. The program should allow the user to enter a persons name (or part of a persons name). It should then search for that person in the people array. If the person is found, it should get that persons phone number from the phone numbers array and display it. If the person is not found in the people array, the program should display a message indicating so.
Code:
Def main:
#declare variables
Size=7
People=[any 7 common names]
Phonenumbers= [7 phone numbers]
Searchvalue = 0
index = 0
Found = false
Inputpeople = 0
Found =false
#get name from user
InputpeopleInputpeople = raw_input("Enter name you are looking for:"
While found ==false and index >= size-1
If (people[index])== phonenumbers[index]:
Found == true
Else:
Index = index +1
If found ==1:
Print"the phone number is ",phonenumbers
Else:
Print"there is no listing for this number"
Main()
There are a few things you can do. The simplest thing would be the .index method. Take the string you got from the input and the name array and do something like this:
ind = people.index(inputtedName)
However, you can also continue with the algorithm you were making. Since this is a homework assignment, I recommend this. The loop you have is good. You need to search through all the names. However, you are comparing the name at index index
to the phone number at index index
. This doesn't make sense because the phone numbers and the names shouldn't be the same. Instead, compare the name to the inputted name. If they match, then the index you are on is the index that the user wants.
Then you need to display the phone number at that index instead of all of the phone numbers like you are doing now.
Finally, remember to capitalize correctly. Booleans are capitalized in python, most functions and such are not.
I can give more detail if you need, but hopefully this is enough.