Filter Variables with Python (Python Part 15)

This class explains the basic concepts of filtering user input with Python and why it matters. The lab shows how to create a loop that requests text that is formatted properly for the scripts needs.

  • 00:00 Intro 03:56
  • Why Filtering Maters
  • 10:29 What About RegEx
  • 12:39 Filtering Names, Ages, Phone Numbers, Email Address
  • 30:44 Final Thoughts

filter.py

def get_name():
  while True: 
    name = input('Name: ')
    if name.isalpha():
      return name
    else:
      print('Only Letters are allowed for names')
      
def get_age():
  while True: 
    age = input('Age: ')
    try:
      age = int(age)
      return age
    except:
      print('Only Numbers are allowed for Age')

def get_phone():
  while True:
    phone = input('Phone Number: ')
    phone = phone.replace('-','')
    if len(phone) == 10 or len(phone) == 11:
      try:
        phone = int(phone)
        return phone
      except:
        print("Phone Numbers must Numeric digits")
    else:
      print('Phone Numbers must be 10 - 11 Digits')

def get_email():
  while True:
    email = input('Email Address: ')
    if '@' in email and '.' in email:
      return email
    else:
      print("Please enter a valid email address")

def get_password():
  while True:
    password = input('Password: ')

user_name = get_name()

user_age = get_age()

user_phone = get_phone()

user_email = get_email()


print('---- USER INFO ---')

print(user_name)

print(user_age)

print(user_phone)

print(user_email)

Be the first to comment

Leave a Reply