Final Exam Review Spring 2004: Handling an address book
Link back to Sp2004 Final Exam Review
Questions and Answers?
Hmmm...this one's alot like "Tool for the TAs"
def contact(keyword):
contactList = getMediaPath("contacts.txt")
list = open(contactList, "rt")
contacts = list.read()
list.close()
conloc = contacts.find(keyword)
if conloc != -1:
keyloc = contacts.rfind("/", 0, conloc)
endline = contacts.find("\n", keyloc)
print conloc[keyloc:endline]
if conloc == -1:
print "Not found"
I don't know what's wrong with this code.. I keep getting this error message when i try to run it..
>>> contact("king")
You are trying to access a part of the object that doesn't exist.
in file C:\Documents and Settings\sana yousufi\My Documents\CS 1315\New Folder\contact.py, on line 10, in function contact
AttributeError: __getitem__
Please check line 10 of C:\Documents and Settings\sana yousufi\My Documents\CS 1315\New Folder\contact.py
Sana Yousufi
Jasmina Pacariz
def contact(string):
sequencesFile=getMediaPath("contacts.txt"
file=open(sequencesFile, "rt")
sequences=file.read()
file.close()
stringloc=sequences.find(string)
if stringloc != -1:
search1=sequences.rfind("#", stringloc)
search2=sequences.find(",",search1)
search3=sequences.rfind(":", search2)
print, sequences[search1:search3]
if stringloc== -1:
print "Not found"
oops forgot to close the close the parenthesis on 2nd row. Jasmina Pacariz
| Sana, you're trying to print a slice ([keyloc:endline]) of the number, conloc. Maybe you meant "contacts" there? Mark Guzdial |
def contact(keyword):
contactList = getMediaPath("contacts.txt")
list = open(contactList, "rt")
contacts = list.read()
list.close()
conloc = contacts.find(keyword)
if conloc != -1:
keyloc = contacts.rfind("#", 0, conloc)
endline = contacts.find("\n", keyloc)
print contacts[keyloc:endline]
if conloc == -1:
print "Not found"
Sana Yousufi
That code is giving me errors when I try to use it. It says:
">>> contact(king)
A local or global name could not be found. You need to define the function or variable before you try to use it in any way."
And it's loaded and everything...
??
This code works:
def contact(keyword):
addressbook = getMediaPath("contacts.txt")
file = open(addressbook, "rt")
contacts = file.read()
file.close()
keyloc = contacts.find(keyword)
if keyloc == -1:
print "Not found!"
if keyloc != -1:
start = contacts.rfind("#", 0, keyloc)
end = contacts.rfind(":", 0, keyloc)
print contacts[start+1:end]
why the "+1"?
| contact(king) should be contact("king"). To figure out the "+1" – try it without it. Mark Guzdial |
Hi,
I don't understand a part of the question. Does the input string have to be a keyphrase or can it be anything like the name, company etc? because if it can be anything, then the above code that works, will not.
I'm looking at the code above that works, and what i don't understand is why the starting index is 0. Does the index reset itself to 0 everytime you're on a new line? How can you use rfind with the start index as 0? wont it save the index of the 1st ":" on that particular line?
oh an another question. If i use seg.find(":",keyloc), will it find the 1st colon after keyloc and then stop?
How come in this program the loop is:
if keyloc != -1:
start = contacts.rfind("#", 0, keyloc)
end = contacts.rfind(":", 0, keyloc)
print contacts[start+1:end]
But in the help for the TAs program it is
if stuloc != -1:
first = students.find("/", stuloc)
second = students.find("/", first+1)
third = students.find("/", second+1)
endofline = students.find("\n", third+1)
print students[third+1:endofline]
Why does one have the 0s and the other doesn't? And what do the 0s do?
In the book it states that the definition for .rfind is "does the same thing as find but it looks backwards" (pg 217)
Mark, i looked up .find and .rfind but the text doesn't say enough to answer my questions. Does rfind/find stop looking as soon as it finds the 1'st colon?
if it does stop,then what does the 0 mean in std.rfind(":",0,keyloc)?
0 is not necessary, is it? Or does the 0 refer to the 1'st colon it finds?
| If .find has more than one argument, the first one is the string to look for, the second one is the index where to START looking, and the third one is the index where to STOP looking. rfind works exactly the same, but it looks from right to left within the range START->STOP. So, std.rfind(":",0,keyloc) means to look from the location keyloc to the beginning of the string (0) but from keyloc towards 0. students.find("/",stuloc) says to look FORWARD for "/" from index stuloc towards the end of the file (presumed if not explicitly specified). Yes, it returns the index of THE FIRST instance of the string that it finds. Mark Guzdial |
what are the parameters for rfind?
My code:
def breakout(keyword):
contactsfile = getMediaPath("contacts.txt")
file=open(r"C:\JES\contacts.txt", "rt")
people = file.read
file.close
curloc = people.find(keyword)
if curloc == -1:
print "Not Found"
if curloc != -1:
end = contacts.rfind(":", curloc)
nameloc = contacts.rfind("#", end)
print people[nameloc:end]
It's highlighting this line:
curloc = people.find(keyword)
and giving me this error:
You are trying to access a part of the object that doesn't exist.
in file C:\JES\finaladdress.py, on line 6, in function breakout
AttributeError: 'method' object has no attribute 'find'
Please check line 6 of C:\JES\finaladdress.py
What's the prob. here? I don't know why find wouldn't work here.
Jonathan Laing
def contact(keyword):
files = getMediaPath("contacts.txt")
file =open(files, "rt")
word = file.read()
file.close()
wordloc = word.find(keyword)
if wordloc <> -1:
keyloc = word.rfind("#",0,wordloc)
endline = word.find("/n",keyloc)
print word[keyloc:endline]
if wordloc == -1:
print "not found"
it's saying "A local or global name could not be found. You need to define the function or variable before you try to use it in any way." when i run my code. i've set the media path, and u can't figure out why.
Nicole Maser
Am I understanding this right? Are these the parameters of find and rfind?
find(findstring, start, end) , Going left to right (forward)
rfind(findstring, end, start) , Going right to left(backwards).
Jonathan Laing
| Jonathan, you are assigning people to the FUNCTION people.read, not EXECUTING the function with people.read(). Nicole, I'm not seeing the error. What line? Did you load first? Jonathan, you could test .find and .rfind and see if your understanding works. Mark Guzdial |
why does this also print the keywords???
def contact(keyword):
files = getMediaPath("contacts.txt")
file =open(files, "rt")
word = file.read()
file.close()
wordloc = word.find(keyword)
if wordloc <> -1:
keyloc = word.rfind("#",0,wordloc)
endline = word.find("/n",keyloc)
print word[keyloc:endline]
if wordloc == -1:
print "not found"
Karin Bowman
can you do this with "find....." instead of "rfind....."?
Link to this Page