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

Gotta be a better way

Hi,

I'm trying to make some validation on the user input for my addressbook and it's working at the moment, but I have a feeling that there's a better way to do this.

Right now I'm using a while loop for each of the informations (first name, last name, phone number and email).

Any suggestions how to avoid spamming those while loops?

Here is the code:

        while True:
            self.first_name = input("Please enter the first name: ").title()
            is_name = re.match(r'[a-zA-Z- ]+', self.first_name)

            if is_name:
                break
            else:
                print("That's not a valid name!")
                continue

        while True:
            self.last_name = input("Please enter the last name: ").title()
            is_name = re.match(r'[a-zA-Z- ]+', self.first_name)

            if is_name:
                break
            else:
                print("That's not a valid name!")
                continue

        self.address = input("Please enter the address: ").title()

        while True:
            self.phone_number = input("Please enter the phone number: ")
            is_phone = re.match(r'[0-9()\-+ ]+', self.phone_number)

            if is_phone:
                break
            else:
                print("That's not a valid phone number!")
                continue

        # checking if valid email address
        # checking if the email is already in the database
        # asking for another email if already in database
        while True:
            self.email = input("Please enter the email: ")

            is_email = re.match(r'[-\w\d.+]+@[-\w\d.]+', self.email)

            if is_email:
                query = Contact.select().where(Contact.contact_email == self.email)

                if query.exists():
                    print("That email is already in use. Please enter another email.")
                    continue
                else:
                    break
            else:
                print("That's not a valid email!")
                continue

1 Answer

I would suggest using functions instead of while loops, for example

def first_name():
            self.first_name = input("Please enter the first name: ").title()
            is_name = re.match(r'[a-zA-Z- ]+', self.first_name)

            if not is_name:
                print("That's not a valid name!")
                first_name()

You don't need to check if the name is valid again, you only need to go back to the top when the name is invalid. I would always try to use functions over these while loops because you can reuse them. On another note: in you first while loop you can omit the 'continue' because the loop will start from the top anyway, but it isn't that important.

This helped cleaning the code A LOT, thank you! :-)

I noticed the names would accept numbers as long as the names began with a letter, and the phone number would accept letters as long as the number began with a number :-p fixed this by changing re.match with re.fullmatch :-p