Midterm Exam 2 Review Fall 2004: Address book functions
Post answers, comments, and questions here!
(Back to Fall2004 Midterm 2 Review)
am i on the right path???
def lookup(string):
address=open("address.txt","rt")
phonelist=address.split('\n')
newphonelist=[]
for person in newphonelist:
if person[0]==string:
print person[0:3]
if person[0]<>string:
print "Not Found"
def phone(string):
address=open("address.txt","rt")
phonelist=address.split('\n')
newphonelist=[]
for person in newphonelist:
if person[0]==string:
print person[3]
if person[0]<>string:
print "Not Found"
| Looks close – does it work? Did you try it? (Read FAQ on how to format your code so that we can read it.) Mark Guzdial |
Robert.. you are so smart :-/
def phonebook():
return """Charlie Brown:1919 Peanuts Lane:Atlanta, GA:404-992-9292
Peppermint Patty:2020 Cashew Street:Atlanta, GA:404-299-2929
Calvin:101 Tiger Lane:Decatur, GA:770-899-8989"""
def phone():
phones = phonebook()
phonelist = phones.split('\n')
newphonelist = []
for list in phonelist:
newphonelist = newphonelist + [list.split(':')]
return newphonelist
def lookUp(person):
notFound = 1
for people in phone():
if people[0].find(person)<>-1:
print people[0:]
notFound = 0
if notFound == 1:
print "Not Found"
that is from casey white! it works perfectly!
you can enter whole names, partial names, partial numbers...anything...and it will return all the possibilities
def phonebook():
return """Charlie Brown:1919 Peanuts Lane:Atlanta, GA:404-992-9292
Peppermint Patty:2020 Cashew Street:Atlanta, GA:404-299-2929
Calvin:101 Tiger Lane:Decatur, GA:770-899-8989"""
def phone():
phones = phonebook()
phonelist = phones.split('\n')
newphonelist = []
for list in phonelist:
newphonelist = newphonelist + [list.split(':')]
return newphonelist
def lookUp(person):
notFound = 1
for people in phone():
if people[0].find(person)<>-1:
print people[0:]
notFound = 0
if notFound == 1:
print "Not Found"
there...easier to read
to return just the phone number, I switched print people[0:]
to print people[3]
what does the % sign do in JES?
when writing functions, we're allowed to write multiple ones and call on them in the main function?
the code at the top has an error that reads "AttributeError: instance of 'org.python.core.PyFile' has no attribute 'split'"...how does split not exist??
| There is no split() method for python file objects. open is a python keyword for creating a file object. However, file objects have a read() method which will return a string containing the contents of the file object. Missing Page |
you can easily combine it into one, and also you can make it take in a directory by using something like file.read()
...casey
If you want everything in one function, here it is:
def lookup(string):
programfile = "address.txt"
newfile = open(programfile,"rt")
addresses = newfile.read()
newfile.close()
phonelist = addresses.split('\n')
newphonelist = []
for list in phonelist:
newphonelist = newphonelist + [list.split(":")]
notFound = 1
for people in newphonelist:
if people[0].find(string)<>-1:
print people[0:]
notFound = 0
if notFound == 1:
print "Not Found"
Link to this Page