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 PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

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 just 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"

Andrew Wong

That number is not Charlie Browns. 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 used a variable besides "person", because the lines divide by person, but person[0] and person[1] are different pieces of info about the same person.



Link to this Page