The Python program of the Username Generator is shown below
import re
# Function to validate name (should not contain digits)
def validate_name(name):
return bool(re.match("^[a-zA-Z]+$", name))
# Function to validate campus
def validate_campus(campus):
valid_campuses = ["Johannesburg", "Cape Town", "Durban", "Phokeng"]
return campus in valid_campuses
# Function to validate cohort year (not in the past)
def validate_cohort_year(cohort_year):
current_year = 2023 # Assuming the current year is 2023
return cohort_year >= current_year
# Function to generate the username
def generate_username(first_name, last_name, campus, cohort_year):
# Format the campus code
campus_codes = {"Johannesburg": "JHB", "Cape Town": "CPT", "Durban": "DBN", "Phokeng": "PHO"}
campus_code = campus_codes.get(campus, "")
# Extract the first three letters of the last name
last_name_part = last_name[:3].upper().ljust(3, 'O')
# Extract the last three letters of the first name
first_name_part = first_name[-3:].upper().ljust(3, 'O')
# Combine all parts to create the final username
final_username = f"{first_name_part}{last_name_part}{campus_code}{cohort_year}"
return final_username
# Main program
def main():
# Input personal information
first_name = input("Enter your First Name: ")
last_name = input("Enter your Last Name: ")
campus = input("Enter your Campus (Johannesburg, Cape Town, Durban, Phokeng): ")
cohort_year = int(input("Enter your Cohort Year: "))
# Validate input
if not (validate_name(first_name) and validate_name(last_name) and validate_campus(campus) and validate_cohort_year(cohort_year)):
print("Invalid input. Please check your input and try again.")
return
# Generate the username
final_username = generate_username(first_name, last_name, campus, cohort_year)
# Confirm the final username
print("\\Final username:")
print(final_username)
confirmation = input("Is the final username correct? (yes/no): ")
if confirmation.lower() == "yes":
print("Username confirmed! Welcome to the bootcamp.")
else:
print("Please run the program again to generate the correct username.")
if __name__ == "__main__":
main()
So, the program is one that takes user input, validates it according to the specified criteria, generates a username, and asks the user to confirm if the final username is correct.