Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Jose Soto
23,407 PointsRegular Expressions in Python Extra Credit
I took a stab at the extra credit for regular expressions in python. Here is what I came up with:
person.py
class Person:
def __init__(self, first_name, last_name, email, phone, job, twitter):
self.first_name = first_name
self.last_name = last_name
self.email = email
self.phone = phone
self.job = job
self.twitter = twitter
address_book.py
class AddressBook:
person_list = []
search_result = []
def __init__(self):
return
def add(self, person):
self.person_list.append(person)
def search(self, search_string):
self.search_result = []
for person in self.person_list:
if search_string in (person.first_name or
person.last_name or
person.last_name or
person.email or
person.phone or
person.job or
person.twitter):
self.search_result.append(person)
self.print_search_result(search_string)
def print_search_result(self, search_string):
if len(self.search_result) == 0:
print("No Results for \"{}\".".format(search_string))
for person in self.search_result:
print ('''
Name: {} {}
Email: {}
Phone: {}
Job: {}
Twitter: {}
'''.format(person.first_name, person.last_name, person.email,
person.phone, person.job, person.twitter))
main.py
import re
from address_book import AddressBook
from person import Person
address_book = AddressBook()
def search_tool(field):
search_string = input('Searching {} in our Address Book:\n'.format(field))
address_book.search(search_string)
names_file = open("names.txt", encoding="utf-8")
data = names_file.read()
names_file.close()
line = re.compile(r'''
^(?P<name>
(?P<last_name>[-\w ]*),\s # Last name
(?P<first_name>[-\w ]+) # First name
)\t # Last and first names
(?P<email>[-\w\d.+]+@[-\w\d.]+)\t # Email
(?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t # Phone
(?P<job>[\w\s]+,\s[\w\s.]+)\t? # Job and company
(?P<twitter>@[\w\d]+)?$ # Twitter
''', re.X | re.M)
for match in line.finditer(data):
first_name = match.group('first_name')
last_name = match.group('last_name')
email = match.group('email')
phone = match.group('phone')
job = match.group('job')
twitter = match.group('twitter')
temp_person = Person(first_name, last_name, email, phone, job, twitter)
address_book.add(temp_person)
while True:
category = input('''
What would you like to search?
[F]irst name, [L]ast name, [E]mail, [P]hone number, [J]ob, [T]witter
Type 'Exit' to exit.
''')
if category.upper() == "F":
search_tool("First Name")
elif category.upper() == "L":
search_tool("Last Name")
elif category.upper() == "E":
search_tool("Email")
elif category.upper() == "P":
search_tool("Phone number")
elif category.upper() == "J":
search_tool("Job")
elif category.upper() == "T":
search_tool("Twitter")
elif category.upper() == "EXIT":
break
else:
print('Not a valid response. Please try again.')
If you run the application, you will notice that some of the search functionality does not work properly. However, it works well enough that I feel comfortable with the subject.
If you happen to improve on this code, please share it with me. Thanks!
2 Answers

Iain Simmons
Treehouse Moderator 32,303 PointsIn your address_book.py
file, I think you're using the if ... in
control structure incorrectly. The part after the in
should be a list (or at least some iterable).
def search(self, search_string):
self.search_result = []
for person in self.person_list:
if search_string in (person.first_name or
person.last_name or
person.last_name or
person.email or
person.phone or
person.job or
person.twitter):
self.search_result.append(person)
self.print_search_result(search_string)
Try using square brackets and commas to separate the address book 'fields'.

Jose Soto
23,407 PointsThanks Lain. I hadn't thought of that. I appreciate the input.