diff --git a/Address Validator/.gitignore b/Address Validator/.gitignore new file mode 100644 index 00000000..e43b0f98 --- /dev/null +++ b/Address Validator/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/Address Validator/AddressValidator.py b/Address Validator/AddressValidator.py index 0dc49be1..b937067d 100644 --- a/Address Validator/AddressValidator.py +++ b/Address Validator/AddressValidator.py @@ -1,16 +1,42 @@ +import time + + +''' +Fixes: + - Now checks number of '@' symbols + - Now checks if characters come before and after the '@' symbol + - Now checks position of '.' +''' + def addressVal(address): - dot = address.find(".") - at = address.find("@") - if (dot == -1): - print("Invalid") - elif (at == -1): - print("Invalid") - else: - print("Valid") + """ + Validates an email address. + Ensures only one '@' is present, the '@' is the not the first char, + there is atleast one '.' after the '@', and the '.' is not the last + char in the email. + + Args: + address (str): The email to be validated. + Returns: + str: "Valid" if the email is valid, an invalid message otherwise. + """ + if '@' not in address or address.count('@') != 1: + return "Invalid: Email must contain exactly one '@' symbol!" + + at_pos = address.find('@') + dot_pos = address.find('.', at_pos) + + if at_pos == 0 or at_pos == len(address) - 1: + return "Invalid: Characters must appear before or after the '@'!" + + if dot_pos == -1 or dot_pos == len(address) - 1 or dot_pos < at_pos + 2: + return "Invalid: Dot must come after the '@'!" + + return "Valid" + print("This program will decide if your input is a valid email address") while(True): - print("A valid email address needs an '@' symbol and a '.'") x = input("Input your email address:") - - addressVal(x) + result = addressVal(x) + print(result)