4.9k views
5 votes
Create a program in python

Username Generator
A feature that generates a unique bootcamp username based on a format and
personal information.
The program should be structured in the following way:
1. Your program should prompt a user to input Their First Name, Last Name,
Campus and the cohort year they are entering. - It is your choice how you will
expect this input, one by one or in a single string
2. Your program should validate user input in the following ways:
a. First name and last name name should not contain digits
b. Campus should be a valid campus
c. Cohort year should be a valid cohort year - a candidate can’t join a cohort
in the past
3. You will have a function that produces the username from the input provided.
4. The user will then be asked if the final username is correct. Let them know what
the format of the username is and if the final username is correct.
See below for an example of the final bootcamp username based on personal
information:
First Name: Lungelo
Last Name: Mkhize
Cohort Year: 2022
Final Campus: Durban
Final username:
elomkhDBN2022
ELO - Last 3 letters of first name (if their name is less than 3 letters you should add the
letter O at the end)
MKH - First 3 letters of their last name (if their name is less than 3 letters you should
add the letter O at the end)
DBN - Final Campus selection - Johannesburg is JHB, Cape Town is CPT, Durban is DBN,
Phokeng is PHO
2022 - The cohort year they are entering

1 Answer

1 vote

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.

User Romz
by
9.0k points