In: Computer Science
In python, how do I define a function (check_domain) which takes a email (string) and domain name (string) as input. using slice, it will return true if the email address' domain same matches the input domain name. For example, when input are ”[email protected]” and ”msn.com”, the function you defined should return True. When input are ”[email protected]” and ”hotmail.com”, it should return False
Steps:
· email and domain name were got as input
· check_domain() is called the return value is checked using if
· if the return value is true then email domain matches with the domain name given
· otherwise does not matches.
· check_domain() function takes email and the domain name as parameters
· using the split function it splits the email by @ symbol
· dom has the list of literals seperated by the @ symbol
· dom[0] will have mail id and dom[1] will have the domain name
· therefore dom[1] is compared with domain name passed and if it is equal it sends true
· otherwise false.
Code:
#check_domain() function takes email and the domain name as parameters
# using the split function it splits the email by @ symbol
# dom has the list of literals seperated by the @ symbol
#dom[0] will have mail id and dom[1] will have the domain name
# therefore dom[1] is compared with domain name passed and if it is equal it sends true
# otherwise false
def check_domain(email,domain):
dom=email.split("@")
if domain==dom[1]:
return True
else:
return False
# email and domain name were got as input
# check_domain() is called the return value is checked using if
# if the return value is true then email domain matches with the domain name given
# otherwise does not matches.
email=input("Enter Email : ")
domain=input("Enter Domain Name : ")
if check_domain(email,domain):
print(email + " matches the " + domain)
else:
print(email + " does not matches the " + domain)
Screenshot:

Output:
Enter Email : [email protected]
Enter Domain Name : msn.com
[email protected] matches the msn.com
Enter Email : [email protected]
Enter Domain Name : hotmail.com
[email protected] does not matches the hotmail.com


Do follow the indentation of the code as in the screenshot. In Python indentation matters a lot.