Hey I'm new to Python and taking a class on it.
To get started here is my homework specs
Sequential Letter Search
1.You will design and implement a program that will:
i.Accept as input a string of letters that will be looked up in another string.
ii.For each of the letters in the input string you will search the other string, called phonebook (this string is provided) starting from the first position and moving down the phonebook string until either the letter has been found, or the letter was determined not found. At which point, you will print the letter, whether it was found or not found, and the number of compares necessary to complete the search.
iii.After you have completed all searches you will provide statistics for both the success and failure rates (ie: number success = xx, total compares yy, average z.z / number failures = aa, total compares = bb, average c.c)
b.Save the code for this script/program in a file named: ssearch.py
c.Note: you are to write the search code yourself, do not use built-in string functions.
d.phonebook = ‘bcdfghjklmnpqrstvwxyz’
e.you may assume that phonebook will be in ascending order and will not contain duplicates.
‘a’ not found with 1 compares
‘b’ found with 1 compares
number success = 1; total compares = 1; average = 1.0
number failures = 1; total compares =1; average = 1.0
‘d’ found with 3 compares
‘o’ not found with 12 compares
‘g’ found with 5 compares
number success = 2; total compares = 8; average = 4.0
number failures = 1; total compares =12; average = 12.0
Now I'm also rather new to programming in general, so I'm not quite sure how to get this looping concept to work correctly.
Here's what I have (unsuccessfully) came up with:
inp = (raw_input('Input: ')) phonebook = 'bcdfghjklmnpqrstvwxyz' count = 0 for char in phonebook: if char == inp[count]: print inp[count] + ' found' count= count + 1 else: print inp[count] + ' not found' count=count + 1
I keep recieving fatal errors, but it "works" if I change inp[count] to inp (albeit it will only return results for the first string.
I don't even know where to begin with the rest of the output. I'm not trying to ask for the solution, but I really need someone who knows more about programming about how to structure my code.
Once I know how the structure should be I should be able to figure it all out.
Thanks for the help :I