Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Final Exam Review Fall 2003: Handling an address book

Questions, comments, answers?

Back to Fall2003 Final Exam Review


def contact(keyword):
  contactsfile = getMediaPath("contacts.txt")
  file = open(contactsfile,"rt")
  contacts = file.read()
  file.close()
  keywordloc = contacts.find(keyword)
  if keywordloc == -1:
    print "Not Found"
  if keywordloc <> -1:
    beginloc = contacts.rfind("#", 0, keywordloc)
    firstcolon = contacts.find(":", beginloc)
    secondcolon = contacts.find(":", firstcolon + 1)
    thirdcolon = contacts.find(":", secondcolon + 1)
    print contacts[beginloc + 1: thirdcolon]


There is an easier way (than the above program). Reverse search from the located keyword to the last colon. From this colon, reverse search for the # symbol. Thus, you don't have to find each colon in an entry. #12

Question: phonebook.rfind(":", 0, location)...what does the zero (used with reverse finds) mean? My program does not run properly without it, but I really don't understand its purpose. #12

Nice call, Cassy! I find the inputs to rfind non-intuitive, too. The two optional inputs indicate the endpoints for the search, where the smaller value must come first. So, if you want to search backward from a particular location you have to also specify how far back to search – that's the 0. That's why rfind(":",0,location)–we're searching backward from location to 0. Mark Guzdial

Cassy, could you post an examply of your code? That would be so helpful! I just don't quite understand how you would integrate rfind() here.

I hope this helps. #12
end = contacts.rfind(":",0,location)
start = contacts.rfind("#",0,end)

is it just me or does this test seem easier then the first two????

I am a little confused about something. Here is the second part of the code:
if names<>-1:
line=contact.find("\n", names)
endcolon=contact.rfind(":", 0, line)
print contact[names:endcolon]

Is that correct? I am also wondering where print would start, since names could be anywhere in the string, right?

What if it looks like this:

if names<>-1:
line=contact.find("\n", names)
endcolon=contact.rfind(":", 0, line)
begin=contact.rfind("#", 0, endcolon)
print contact[begin+1:endcolon]

Thank you!



def contact(query):
	file = "contacts.txt" #if in media folder getMediaPath(...)
	input = open(file,"rt")
	addressbk = input.read()
	input.close()
	
	loc = addressbk.find(query)
	if loc == -1:
		print "Not found"
	if loc <> -1:
		startloc = addressbk.rfind('#',0,loc)
		endloc = startloc
		for i in range(1,4):
			endloc = addressbk.find(':',endloc+1)
		print addressbk[startloc+1:endloc]

houman

my code looks like the one on the top of this page (my vaiables have different names, but it's the same code), but when i try to execute it, i set the media path and then do contact(king) and it tells me that a local or global name could not be found. am i just doing something simple that's wrong, because that code should work.

ok, here's mine.
def contact(keyword):
  addbooksFile=getMediaPath("contacts.txt")
  file=open(addbooksFile,"rt")
  addbooks=file.read()
  file.close()
  wordloc=addbooks.find(keyword)
  if wordloc &lt> -1:
    startloc=addbooks.rfind("#", 0, wordloc)
    midloc1=addbooks.find(":",startloc)
    midloc2=addbooks.find(":",midloc1 + 1)
    endloc=addbooks.find(":",midloc2 + 1)
    print addbooks[startloc + 1:endloc]
  if wordloc == -1:
    print "Not Found!"

Sara Jones

If you have a "local or global name not found," I hope you know how to figure it out now: Put expert mode on to see what word it is, make sure that you have loaded, check the line and all the names on that line, etc. Mark Guzdial


can someone explain why it's <>? (on line if wordloc <> -1:)
also wy do you add +1 (on lines midloc2=addbooks.find(":",midloc1 + 1))
endloc=addbooks.find(":",midloc2 + 1)

lastly, why is there a +1 (on line print addbooks(startloc + 1:endloc))??
== means 'equal to' and <> means 'does not equal to' Angela Liang

Sara is adding one to startloc because she found that if she printed it from startloc (without adding one), she was printing the colon, too. By adding one, she skips the colon. Mark Guzdial


def contact(keyword):
  contactFile = open("contacts.txt")
  contacts = contactFile.read()
  contactFile.close()
  keywordFound = contacts.find(keyword)
  if keywordFound == -1:
    print "Not Found"
  if keywordFound != -1:
    contactStart = contacts.rfind("#",0,keywordFound)
    contactEnd = contacts.rfind("/n",0,keywordFound)
    phoneEnd = contacts.rfind(":",0,contactEnd)
    print contacts[contactStart+1:phoneEnd-1]


Why would you use rfind to find the END of the line? Have you tried it to see if it works? (Consider reading the FAQ on posting code legibly...) Mark Guzdial


yeah it works


i modified my contactEnd to contactEnd = contacts.find("/n",contactStart), works the same

instead of having two if statements, couldnt you just put a return in the keyword == -1 if statement so that it wills top if it doesnt find the search item but will continue if it does. Somethign like this?

keywordloc = contacts.find("keyword")
if keyword == -1:
    print "Not Found!"
    return
startloc = contacts.rfind("#", 0, keywordloc)
endlineloc = contacts.find("/n", keywordloc)
endloc = contacts.rfind(":", 0, endlineloc)
print contacts[startloc+1:endloc-1]


I mean, I guess it doesnt really matter if you have that second if statement or not, I just figured it would be another way to do it this way. Plus if it doesnt equal -1 then it will jsut continue, why have a statment that asks if it isnt equal to -1 becasue we know that if it got past that first if statement then it wasnt equal to -1. My penny's worth.

Geoff Sosebee

Nice one, Geoff! Mark Guzdial


def contacts(keyword):
  file="contacts.txt"
  contactsFile=open(file,"rt")
  contacts=contactsFile.read()
  contactsFile.close()

  location=contacts.find(keyword)
  if location <>-1:
    end=contacts.rfind(":",0,location)
    beg=contacts.rfind("#",c,end)
    print addresses [beg+1:end]
  if addressloc == -1:
    print "Not Found."

Does this look like the simplified version?? Is the second line right or do we have to do getMediaPath?


Oops, the "c" in the fourth to last line should be a 0

def contact(keyword):
  contactsfile = getMediaPath("contacts.txt")
  file = open(contactsfile,"rt")

What does the "rt" do?


"rt" means "read as text". I could be wrong. Double check that. #12

def contact(keyword):
  file=getMediaPath("contacts.txt")
  openfile=open(file,"rt")
  contacts=openfile.read()
  openfile.close()

conloc=contacts.find(keyword)
if conloc == -1:
  print "Not Found"
if conloc <> -1:
   startloc=(":",0,conloc)
   endlineloc=("/n",conloc)
   endloc=(":",0,endlineloc)
   print contacts[startloc+1:endloc-1]


what lecture slides is this question/these answers coming from?


I have used the ppt titled "File Encoding" or more specifically, "Files: how to read them, write them, and process lots of them" #12

Link to this Page