diff --git a/customer_adoption_form_dog.py b/customer_adoption_form_dog.py index ed207fd..c66caab 100644 --- a/customer_adoption_form_dog.py +++ b/customer_adoption_form_dog.py @@ -103,12 +103,7 @@ def adopt_dog_form(): garden_fenced = get_input("Is your garden fully fenced? (yes/no): " + Style.RESET_ALL).strip().lower() garden_access = get_input("How is the garden accessed? " + Style.RESET_ALL).strip() - elif garden == "communal": - garden_size = "N/A" - garden_fenced = "N/A" - garden_access = "N/A" - - elif garden == "no": + elif garden == "communal" or garden == "no": garden_size = "N/A" garden_fenced = "N/A" garden_access = "N/A" diff --git a/register.py b/register.py index 47dd074..5b2807d 100644 --- a/register.py +++ b/register.py @@ -14,65 +14,64 @@ def register(): clear_screen() while True: - # Prompt user to enter a username - username = input("\nEnter a username: ").strip() + username = get_username() + if username is None: + continue - if username == "": - clear_screen() - print(Fore.RED + "\nUsername cannot be empty. Please try again." + Style.RESET_ALL) + password = get_password("Enter a password: ") + if password is None: continue - - # Check if username already exists in the database - if users_collection.find_one({'username': username}): - print(Fore.RED + "\nUsername already exists. Please choose another one." + Style.RESET_ALL) - time.sleep(2) - clear_screen() + + confirm_password = get_password("Confirm your password: ") + if confirm_password is None: continue - # Continuous loop for user registration - while True: - # Prompt user to enter and confirm a password - while True: - password = getpass.getpass("Enter a password: ") - if not password.strip(): - clear_screen() - print(Fore.RED + "\nPassword cannot be empty. Please try again." + Style.RESET_ALL) - continue - else: - break - - while True: - confirm_password = getpass.getpass("Confirm your password: ") - if not confirm_password.strip(): - clear_screen() - print(Fore.RED + "\nPassword cannot be empty. Please try again." + Style.RESET_ALL) - continue - else: - break + if password != confirm_password: + print_error_message("\nPasswords do not match. Please try again.") + continue + + user_level = get_user_level() + if user_level is None: + continue - # Check if passwords match - if password == confirm_password: - # Prompt user to enter their user level - user_level = input("Enter your user level (1-3): ") - - # Validate user level input - if user_level.isdigit() and 1 <= int(user_level) <= 3: - # Hash the password - hashed_password = hash_password(password) + hashed_password = hash_password(password) + users_collection.insert_one({ + 'username': username, + 'hashed_password': hashed_password, + 'level': int(user_level) + }) - # Insert user data into the MongoDB collection - users_collection.insert_one({ - 'username': username, - 'hashed_password': hashed_password, - 'level': int(user_level) - }) + print(Fore.GREEN + "\nRegistration successful!" + Style.RESET_ALL) + time.sleep(2) + clear_screen() + return - print(Fore.GREEN + "\nRegistration successful!" + Style.RESET_ALL) - time.sleep(2) - clear_screen() - return - else: - print(Fore.RED + "\nInvalid user level. Please enter a number between 1 and 3." + Style.RESET_ALL) - else: - clear_screen() - print(Fore.RED + "\nPasswords do not match. Please try again." + Style.RESET_ALL) \ No newline at end of file +def get_username(): + username = input("\nEnter a username: ").strip() + if username == "": + print_error_message("\nUsername cannot be empty. Please try again.") + return None + if users_collection.find_one({'username': username}): + print_error_message("\nUsername already exists. Please choose another one.") + time.sleep(2) + clear_screen() + return None + return username + +def get_password(prompt): + password = getpass.getpass(prompt) + if not password.strip(): + print_error_message("\nPassword cannot be empty. Please try again.") + return None + return password + +def get_user_level(): + user_level = input("Enter your user level (1-3): ") + if not user_level.isdigit() or not 1 <= int(user_level) <= 3: + print_error_message("\nInvalid user level. Please enter a number between 1 and 3.") + return None + return user_level + +def print_error_message(message): + clear_screen() + print(Fore.RED + message + Style.RESET_ALL) \ No newline at end of file