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! While you're at it, check out some resources Treehouse students have shared here.

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

Python

Extra credit task of regular expression

Below is my attempt, could you take a look and get back to me some advices?

# Enter any personal info like first name, last name, email, etc.
# Return the whole information of this person

import re
import sys

names_file = open("names.txt", encoding="utf-8")
data = names_file.read()
names_file.close()

class person_info():

    def user_input(self):
        self.search_by = input("Please enter one of the following letters to start your search: "
                           "[L]ast name, [F]irst name, [E]mail, [J]ob, [T]witter: ").lower()
        if self.search_by == 'l':
            self.search_by = 'last'
            self.user_input = input("Please enter the Last name you want to search: ")
        elif self.search_by == 'f':
            self.search_by = 'first'
            self.user_input = input("Please enter the First name you want to search: ")
        elif self.search_by == 'e':
            self.search_by = 'email'
            self.user_input = input("Please enter the Email you want to search: ")
        elif self.search_by == 'j':
            self.search_by = 'job'
            self.user_input = input("Please enter the Job you want to search: ")
        elif self.search_by == 't':
            self.search_by = 'twitter'
            self.user_input = input("Please enter the Twitter you want to search: ")
        else:
            print("Please enter the right character, and try again!")
            self.user_input()


    def search(self):
        line = re.compile(r'''
                    ^(?P<name>(?P<last>[-\w ]*),\s(?P<first>[-\w ]+))\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 commpany
                    (?P<twitter>@[\w\d]+)?$                          # Twitter
                    ''', re.X | re.MULTILINE)
        self.person = {}
        for match in line.finditer(data):
            # print(match.group('last'))
            if self.user_input == match.group(self.search_by):
                self.person = match
            else:
                continue

    def printing(self):
        print(self.person.groupdict())
        again = input("Do you want to search again? Y/N: ").lower()
        if again == 'y':
            person_info()
        else:
            sys.exit()

    def __init__(self):
        self.user_input()
        self.search()
        self.printing()


person_info()

Kenneth Love