Introduction to strings 12/12

Link to lesson

May I please get a reply on why my code is not accepted by Codecademy?

The task:

Copeland’s Corporate Company has finalized what they want their username and temporary password creation to be and have enlisted your help, once again, to build the function to generate them. In this exercise, you will create two functions, username_generator and password_generator.

Let’s start with username_generator. Create a function called username_generator take two inputs, first_name and last_name and returns a username. The username should be a slice of the first three letters of their first name and the first four letters of their last name. If their first name is less than three letters or their last name is less than four letters it should use their entire names.

For example, if the employee’s name is Abe Simpson the function should generate the username AbeSimp.

My solution:

def username_generator(first_name, last_name):
  if len(first_name) <= 3 and len(last_name) >= 4:
    username = first_name + last_name[:4]
  if len(first_name) > 3 and len(last_name) < 4:
    username = first_name[:3] + last_name
  if len(first_name) > 3 and len(last_name) > 4:
    username = first_name[:3] + last_name[:4]
  return username
print(username_generator('Abe', 'Simpson'))

It works in the compiler, I get the AbeSimp, but I get the error from Codecademy which says - “Incorrect: local variable ‘username’ referenced before assignment”

Thank you for any assistance you may provide!

I feel I am dumb :frowning:

I didn’t add the fourth parameter:

def username_generator(first_name, last_name):
  if len(first_name) <= 3 and len(last_name) >= 4:
    username = first_name + last_name[:4]
  if len(first_name) <= 3 and len(last_name) < 4:
    username = first_name + last_name
  if len(first_name) > 3 and len(last_name) < 4:
    username = first_name[:3] + last_name
  if len(first_name) >= 3 and len(last_name) >= 4:
    username = first_name[:3] + last_name[:4]
  return username
print(username_generator('Abe', 'Simps'))

But if there’s a more elegant way to solve this, I’d love to see it