 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Fall 2006 Midterm 2 Review: Address Book
This is impossible....I really don't understand at all...
Stacy Schwaiger
| The function is not that long, it can be written under 10 lines of code. First you need to open a file, read and store all the contents and then close it. After that, you need to split the text into seperate lines. Then, you need a for loop to search for the word, and if you find it, then jREMOVEDt print out that line. - Bobby Mathew |
I'm guessing the key would be splitting on the "\n" and then searching for the string containing the name at the beginning of each item in the list.
def lookup(who):
file = open("address.txt","rt")
addresses = file.read()
file.close
addresslist = addresses.split("\n")
seperatedlist = []
for address in addresslist:
seperatedlist = seperatedlist + [address.split(":")]
x = 0
for person in seperatedlist:
if person[0].find(who) <> -1:
print person[0],":",person[1],":",person[2],":",person[3]
x = x + 1
if x == 0:
print "Not Found"
I split the lines up by colons, so it would only search the names. So, can you turn a list back into a string?
def phone(who):
file = open("address.txt","rt")
addresses = file.read()
file.close
addresslist = addresses.split("\n")
seperatedlist = []
for address in addresslist:
seperatedlist = seperatedlist + [address.split(":")]
x = 0
for person in seperatedlist:
if person[0].find(who) <> -1:
print person[3]
x = x + 1
if x == 0:
print "Not Found"
Student4291
That number is not Charlie REMOVEDs. Its Craig Hightowers. I was dissapointed.
Here's the right answer to the phone function. - Bobby Mathew
def phone(string):
file = open("address.txt","rt")
contents = file.read()
file.close()
lines = contents.split("\n")
for i in lines:
if i.find(string) != -1:
lastColon = i.rfind(":")
print i[lastColon+1:]
return
print "Not found"
I would have REMOVEDed a variable besides "person", becaREMOVEDe the lines divide by person, but person[0] and person[1] are different pieces of info about the same person.
Link to this Page