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

Midterm Exam 2 Review Spring 2004: Address book functions

Comments, answers, and questions go here:

(Link back to Sp2004 Midterm 2 Review)


I dont even know where to start on this one. what does it mean when it says it should be able to take "some part" of the name?
Amelia Cipolla

If I do a search for "alvin," I should find Calvin's entry. I did this one in breakout today, too. Mark Guzdial

is that posted somewhere..(hwat you did in breakout) I am not in that section
Amelia Cipolla

HARD PROB


To what extent do you consider "some part" of a name? It would be a little more helpful to have a set amount, say 4 characters. I mean i could enter "in" and get Peppermint Patty and Calvin.

Who is peppermint patty? I prefer doonesbury myself...
Thomas Sobeck

Below is not tested! Generally though, this is the right idea...
def lookup(something):
  file = open("address.txt","rt")
  contents = file.readlines()
  file.close()
  for line in contents:
    if line.find(something):
      print line
      return # To keep from getting to "Not Found"
  print "Not found!"



i tried running this last program...and it actually prints the next line...? whats going on? James Lytle

if you type "in" and you think it would return 2 names, then that is probably what you want. You should expect the program to return 2 addresss book values.

<pre> 

Ran it through JES and it works---

def lookUp(name):  
  addFile=open(filenameandlocation, "rt")  
  contents=addFile.readlines()
  addFile.close()

  for line in contents:
    if line.find(name) != -1:
      print line
      return # To keep from getting to "Not Found"
  print "Not found!"
  


Right – the earlier solution forgot to check for != -1. Mark Guzdial



Link to this Page