To learn about classes, I chose to create a little something that checks if you have included the following:
- @ sign
- Provider (e.g. 'hotmail')
- Domain extension (e.g. '.com')
- It also checks if you entered spaces in the address and gave the option to keep them or get rid of them.
I would really like some feedback on what I should improve on. If you have any questions about why I did what I did at a specific part of the code, ask away!
class email_checker:
def __init__(self, email_input):
self.email_input = email_input
self.done = False
self.fixed_domain = False
self.redo = True
self.answer = ''
self.domain_extension = ''
self.providers = ['hotmail', 'gmail', 'yahoo', 'outlook']
self.de = ['com', 'org', 'net', 'edu', 'gov']
def enter_email(self):
self.email_input = input('Enter your email \n>')
print('You entered: ' + self.email_input)
self.provider_check()
def provider_check(self):
for i in self.providers:
if i in self.email_input:
self.at_check()
return # I use this to exit this method when it returns from
# self.de_check()
# Making sure that it doesn't run the
# self.enter_email() method again.
print('Please include which provider you\'re using. E.g. outlook')
self.enter_email()
def at_check(self):
if not '@' in self.email_input:
print('----\nYou missed the \'@\' sign! Let me fix that for you.')
for i in self.providers:
if i in self.email_input:
self.email_input = self.email_input.replace(i, '@' + i)
self.space_check()
def space_check(self):
if ' ' in self.email_input:
self.fixed_email = self.email_input.replace(' ', '')
print('----\nPlease choose:\n1: %s\n2: %s' % (self.fixed_email,
self.email_input))
while self.done == False:
self.answer = input('>').upper()
if self.answer == '1'.upper():
self.email_input = self.fixed_email
self.done = True
self.de_check()
elif self.answer == '2'.upper():
self.done = True
self.de_check()
else:
print('Enter \'1\' or \'2\'')
self.done = False
else:
self.de_check()
def de_check(self):
# I tried to use a for loop to loop through the self.de list but
# then the print would print five times. So I went with this route.
if not 'com' in self.email_input and not 'org' in self.email_input \
and not 'net' in self.email_input and not 'edu' in self.email_input \
and not 'gov' in self.email_input:
print('----\nYou forgot your domain extension! Choose one here: \
\n.com\n.org\n.net\n.edu\n.gov')
while self.fixed_domain == False:
self.domain_extension = input('>')
if '.com' == self.domain_extension or '.org' == self.domain_extension \
or '.net' == self.domain_extension or '.edu' == self.domain_extension \
or '.gov' == self.domain_extension:
self.email_input = self.email_input + self.domain_extension.lower()
self.fixed_domain = True
else:
print('Enter one of the above. Don\'t forget the \'.\'!')
else:
pass
if not '.' in self.email_input:
print('----\nYou missed the \'.\' before your domain \
extension! Let me fix that for you.')
for i in self.de:
if i in self.email_input:
self.email_input = self.email_input.replace(i, '.' + i)
if __name__ == '__main__':
email_input = ''
email_checker = email_checker(email_input)
email_checker.enter_email()
print('Here is your email: ' + email_checker.email_input)
Test run:
Enter your email >>>example You entered: example Please include which provider you're using. E.g. outlook Enter your email >>>example hotmail You entered: example hotmail ---- You missed the '@' sign! Let me fix that for you. ---- Please choose: 1: example@hotmail 2: example @hotmail >>>1 ---- You forgot your domain extension! Choose one here: .com .org .net .edu .gov >>>.com Here is your email: [email protected]