Introduction To Strings Review-Different Solution

Hey guys, I am doing the data science career path, and am doing the review for “Introduction to Strings”
I have come to a different conclusion with the code written to answer the question.

  1. Why would they use a for loop for this specifically?
  2. My code doesn’t throw any errors after running a few trials through it. Would my code be a good alternative to using the for loop method? Thanks!

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 user_name. 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.

def username_generator(first_name, last_name):
  user_name = first_name[0:3] + last_name[0:4]
  return user_name

Great work! Now for the temporary password, they want the function to take the input user name and shift all of the letters by one to the right, so the last letter of the username ends up as the first letter and so forth. For example, if the username is AbeSimp, then the temporary password generated should be pAbeSim.

Start by defining a function called password_generator that takes one parameter user_name and defines an empty string named password within the function body.

#Temprorary password function
def password_generator(user_name):
  password = user_name[-1] + user_name[1:-2] + user_name[0]
  return password

#Below is the recommended code

password = ""
  for i in range(0, len(user_name)):
    password += user_name[i-1]
  return password

Here is the 3rd portion of the assignment, yes I am aware it specifically asks to have a for loop used.

Inside password_generator, create a for loop that iterates through the indices of user_name by going from 0 to len(user_name).

The loop should create the password by shifting all the letters of user_name one to the right. To do so, add the letter at the previous index of user_name to password with each pass of the loop.

After the for loop but still within the definition of password_generator, return the password.

Hi @code5001566333 !

Less serious note for personal projects → there are usually more than one way to code for something, if it works it works :grin:

Your code works fine, yes. It’s more about having a robust code snippet for this specific instance.
Reason being, what if in the future you want to change how much it shifts, or if you want to change the direction of the shift. In the for loop, you can just change [i-1] to something like [i+2]. For your alternative, you’d need to change all three user_name slices.

Also, there are plenty of ways to set up code snippets to perform the same function. It depends on what you feel works best. For instance, if you know you will never change the way the password is generated (like when doing a course project), then your code is fine as is. Let’s say you wanted more security with the password, and didn’t want a normal shift, you’d use the loop instead and change this part [i-1] to probably [i+random_num] where random_num is a random integer generated using the random package or a number you pass to the function as a parameter.

Hope this helps, good fortune to you!

2 Likes

That makes so much sense. I got the job done, but we want to be able to make changes. What I did wasn’t inherently wrong, but we want to make updates quick and easy. Thank you!

1 Like