Midterm Exam 2 Review Fall 2003: Address book functions
Answers? Comments? Questions on Answers? Questions on Comments?
(Back to Fall2003 Midterm Review 2)
help please....
nina
| Start from the lecture slides, or the section in the book on doing a phone book with Python. Mark Guzdial |
def lookup(name):
addressfile= "address.txt"
file=open(addressfile,"rt")
adress=file.read()
file.close()
nameloc=address.find("name")
if nameloc!= -1:
namebegin=address.rfind("\n", nameloc)
endloc=address.find("\n",nameloc)
printaddress[namebegin+1,endloc]
if nameloc = -1:
print "not found"
not sure about the 2nd part
| I see a couple of small problems - one is a spacing problem and the other is a a symbol error. Lauren Biddle |
| You might also think about readlines() instead of read()–it makes the code lots easier. You might also use split to break up pieces separated by colons. |
Mark Guzdial|
def phones():
input = open("address.txt")
text = input.read()
input.close()
text = text.split("\n")
book = []
for sample in text:
book = book + [sample.split(":")]
return book
def lookup(name):
found = 1
for entry in phones():
if entry[0].find(name) <> -1:
found = 0
print entry[0],entry[1],entry[2],entry[3]
if found:
print "Not Found!"
def phone(name):
found = 1
for entry in phones():
if entry[0].find(name) <> -1:
found = 0
print entry[0],entry[3]
if found:
print "Not Found!"
Here's what I came up with.
houman
| If you use readlines(), you don't have to split on newlines. Mark Guzdial |
It gives me this error at line "foundString": You are trying to access a part of the object that doesn't exist.
| Because readlines() returns a list of strings, which makes phoneList a list of strings. .find() is a method understood by strings not lists. phoneList.find() won't work. Mark Guzdial |
def lookup(string):
phonebookFile = getMediaPath("address.txt")
phonebook = open(phonebookFile,"rt")
phoneList = phonebook.readlines()
phonebook.close()
for list in phoneList:
foundString = phoneList.find(string)
if foundString <> -1:
firstReturn = phoneList.find("\n",fondString)
endReturn = phoneList.find("\n",firstReturn+1)
print phoneList[firstReturn+1:endReturn]
if foundString == -1:
print "Not Found"
def lookup(name):
addressfile = r"C:\Documents and Settings\Christen\Desktop\address.txt"
file=open(addressfile,"rt")
address=file.read()
file.close()
nameloc=address.find(name)
if nameloc <> -1:
beginloc = address.rfind("\n", 0, nameloc)
endloc=address.find("\n", nameloc)
print address[beginloc+1:endloc]
if nameloc == -1:
print "not found"
this program is JES tested. i dont even care if its right anymore because it works lol...
| JES tested, mother approved? It works, just readlines() is easier. Mark Guzdial |
anyone got anything for the 2nd part???
a)
def lookup(partOfName):
file = "address.txt"
addressFile = open(file, "rt")
addresses = addressFile.read()
addressFile.close()
addressloc = addresses.find(partOfName)
if addressloc != -1:
beg = addresses.rfind ("\n", 0, addressloc)
end = addresses.find ("\n", adressloc)
print addresses [beg + 1:end]
if addressloc == -1:
print "Not Found."
b)
def phone(partOfName):
file = "address.txt"
addressFile = open(file, "rt")
addresses = addressFile.read()
addressFile.close()
addressloc = addresses.find(partOfName)
if addressloc != -1:
end = addresses.find ("\n", addressloc)
beg = addresses.rfind(end, ?:?, addressloc)
print addresses [beg+1:end]
if addressloc == -1:
print "Not Found."
sorry about the indenting
is this right ???
yeah mine looks just like that one..is it right?
for the code above, both of your if statements in part a ask if it equals -1...shouldn't one of them ask if it equals 1?
Why do we have the "\n" in this two functions??
beg = addresses.rfind ("\n", 0, addressloc)
end = addresses.find ("\n", adressloc)
why are the locaters in this order in the parenthesis.
beginloc = address.rfind("\n", 0, nameloc)
endloc=address.find("\n", nameloc)
It seems backwards like it should be endloc = address.find(nameloc,"/n")
| (1) above looks pretty good. (2) – what's the ?:? about? Because of the way that (1) works, you can search for beginloc first or endloc first. In both cases, you're search from the place where the part of the name was found, so you're simply searching forward and backward from there. Mark Guzdial |
I don't understand what the zero means inside: beginning = phonebook.rfind("\n", 0, nameloc)
beginning = phonebook.find("GA:", namefound)
end = phonebook.find("\n",namefound)
print phonebook[beginning + 3:end]
This is how I located the phone number for the second program. Is it okay to use "GA" when I'm searching? I can't just use the colon by itself, because that when begin too early.#12
def phones():
phones=getMediaPath(address.txt")
phonelist=phones.split('\n')
newphonelist=[]
for list in phonelist:
newphonelist=newphonelist + [list.split(":")]
return newphonelist
def lookup(person):
for people in phones():
if people[0] == person:
print people[0,4]
if people[0] != person:
print "Not Found"
ok, ummmm, I don't think this will work, but I'm not sure. I haven't tried it yet. There's something wrong I know, I just don't know what. Are we allowed to use two programms?
partb.
def phone(person):
for people in phones():
#using same as above programm
if people[0]==person:
print people[3]
if people[0]!=person:
print "Not Found"
Oh yeah, how do you do proper spacing on this website?
I am so frusterated...Any feedback?
def phone(partOfName):
file = "address.txt"
addressFile = open(file, "rt")
addresses = addressFile.read()
addressFile.close()
addressloc = addresses.find(partOfName)
if addressloc != -1:
end = addresses.find ("\n", addressloc)
beg = addresses.rfind(end, ?:?, addressloc)
print addresses [beg+1:end]
if addressloc == -1:
print "Not Found."
| See the FAQ on how to get spacing to work right. Why do you need my feedback? JUST GO TRY IT! You have JES. Mark Guzdial |
> beg = addresses.rfind ("\n", 0, addressloc)
> end = addresses.find ("\n", adressloc)
> print addresses [beg + 1:end]
Can someone please explain those three lines? I have no idea why one of them is rfind (instead of find), and I don't know how those two functions work (I don't understand the inputs: "\n", 0, addressloc or "\n", addressloc for find). And why do you have to add a 1 to beg?
- "rfind" is taking three inputs: what you are looking for, where to begin the search, and where to end the search. Although rfind searches from the end to the beginning, when you put in the begin and end points, they need to be in sequential order (yes, it is confusing).
- "find" is taking two inputs: what you are looking for, and where to begin looking. "find" will give you the first location that the thing you want appears at.
Here is an example: Say I want to print only the sequence "ghi" from a string containing the whole alphabet. (There are easier ways to do it, but let's pretend that we have to do it this way.) This is one method to do it:
>>>final=len(alphabet)
>>>startpoint=alphabet.rfind("f", 0, final)
>>>endpoint=alphabet.find("j", 0)
>>>print alphabet[startpoint+1:endpoint]
- The first line defines "final" as the index number where the string ends - the index number of "z".
- The second line searches through the alphabet from the index number 0 ("a") to the index number of 'final' ("z", at index number 26). The search is for the letter "f" - but it begins at "z" and then works towards "a" because it is an rfind. You name the index number of the first "f" that the search finds to be "startpoint".
- The third line searches through the alphabet looking for "j". The search begins at index number 0 ("a"). When it finds the "j", you name the location index number "endpoint".
- The last line then prints the section of the alphabet that falls between the index numbers "startpoint"+1 and "endpoint". You must add one to the "startpoint" because you found the index number of "f", so one after that is where you want to begin - at "g". Since range is up to but not including, although you found the index of "j", no "j" will be printed. It'll only print just the letters with the index numbers after "f" and before "j" -> "ghi".
Lauren Biddle
beg = addresses.rfind ("\n", 0, addressloc)
this means that you are looking for "\n" and you will start at addressloc and go backwards (r means reverse) until you find "\n"
end = addresses.find ("\n", addressloc)
this means that you are trying to find "\n" and you will start looking at addressloc
print addresses [beg + 1:end]
this means that you want to print your results. your result will be the complete line of name address and phone number of your input name.
Thanks Lauren and Mr. Anonymous. :D
IMO, this indexing stuff + internet + database + sound is way too much to cram into one exam. He's going to pick FIVE problems but we have to know EIGHTEEN. What in the world?
Look on the SoapBox Page for tips on how to make this Review a bit more do-able...and if you are really overwhelmed, contact your TA (or me) and set up a last minute one-on-one review session tomorrow before the test. I have time between 11-12 - send me an email! Brittany Selden
WELL IM SO GLAD TAs and Prof. Guzdial check back often to give us tips and explanations! ;]
_
Link to this Page