When should I use slicing?

In this exercise the code that I have written for the first part is this:

def username_generator(first_name, last_name):
first = first_name[:3]
last = last_name[:4]
if len(first_name) > 3 and len(last_name) > 4:
username = first + last
if len(first_name) > 3 and not len(last_name) > 4:
username = first + last_name
if len(last_name) > 4 and not len(first_name) > 3:
username = first_name + last
else:
username = first_name + last_name

Can you pinpoint me the error in this code? Didn’t get the right answer for the first part of the exercise.

For the second part, the right code looks like this:

def password_generator(user_name):
password = “”
for i in range(0, len(user_name)):
password += user_name[i-1]
return password

Wouldn’t this part of the code “password += user_name[i-1]” generate “AbeSimpp” instead of “pAbeSim”? Why?

2 Likes

It’s a bad copy, so no, can’t pinpoint anything in that.

However, I can point out that all of this:

Is completely useless since you already grabbed at most that much

(Codecademy’s solution does something similar, it’s a bit laughable…)

1 Like

You can implement both those functions with one-liners using only slicing (keep in mind that negative indices counts from the back towards the front, so -1 refers to the last character)
If you didn’t just accidentally leave it out when you pasted the code, then your first one is missing a return statement (so your function has no result at all).

2 Likes

def username_generator(first_name, last_name):
first = first_name[:3]
last = last_name[:4]
if len(first_name) > 3 and len(last_name) > 4:
username = first + last
if len(first_name) > 3 and not len(last_name) > 4:
username = first + last_name
if len(last_name) > 4 and not len(first_name) > 3:
username = first_name + last
else:
username = first_name + last_name
return username

The above function was the code that I used. Probably the iteration might have looked as if it’s hard to pinpoint?
According to the exercise, the first three letters from the first name and the first four letters from the last name should form the username of a person, and if that’s not the case, then their entire name should be returned. How does the code above that I have written wouldn’t fit well with the requirement of the exercise?

I mean it’s different. Can’t say what’s wrong in your code, can’t see it. Can’t reproduce the situation.

No, no. It does. But the requirement is to take up to 3 letters right? You did this:

At that point you’re done. (Plus the first four of the last name)
If you’re done, what does the rest do? Nothing.

Hello Surendazz,

We only want to perform slices on inputs where the first name has at least 3 characters, and the last name has at least 4 characters. If either of the the inputs do not meet these requirements, we will not be performing any slicing. So, we want to check if the length of our inputs meet the slicing requirements:

if len(first_name) < 3 or len(last_name) < 4:
username = first_name + last_name
return username

If the length of first_name OR the length of last_name do not meet the slicing requirements, we can skip slicing and simply concatenate the two inputs. For example, if we were to input (“Ty”, “Jordan”) or (“Adam”, “Lee”) or (“Wu”, “Yan”) the function would detect that one or both of the inputs do not meet the slicing requirements. The function would then return “TyJordan”, “AdamLee”, or “WuYan”.

Now, if first_name and last_name meet the length requirements, we can move on to the slicing operations. For those, we concatenate the first 3 letters of the first name with the first 4 letters of the last name:

else:
username = first_name[:3] + last_name[:4]
return username

In this second half of the function, the input data has already passed the length check, so we know that we can perform the slicing work. We can use first_name[:3] to get the first 3 letters of the first name, and last_name[:4] to get the first 4 letters of the last name.

For instance: “Adam”[:3] would be “Ada” and “Jameson”[:4] would be “Jame”, we then concatenate those into “AdaJame” and return the result.

Replying to the second part of the code:

It’s helpful to think about each loop/iteration separately. For the first loop the computer is at the first indice, the A in in AbeSimp and position 0. The command is to add i - 1 to password. 0 - 1 is -1 and -1 is the last indice, p. So now password = ‘p’. On the second loop the computer is at the second indice, the b in AbeSimp and position 1. The command is, again at i - 1 to password. 1 -1 is 0, and 0 is A. So now password = ‘pA’. The loop carries on from there. ‘pAb’, ‘pAbe’, etc

1 Like

Not sure the intent of what you’re doing there and haven’t been able to see codecademy solution, however, I would do the following:

def username_generator(first, last):
tab return first[:3] + last[:4]
def password_generator(username):
tab password = “”
tab for i in range(len(username)):
tab tab password += username[i-1]
tab return password

The reason the second piece of code gives the right answer and not your expected “AbeSimpp” is because “password += something” is the same as “password = password + something” and for each iteration you add your previous iteration’s password plus the new “something”.

Hi arcrunner11706,
Thanks a lot for explaining to me very thoroughly and also taking your time for it. I think I can understand very well for the first part. As for the second part, under this line of code:

password += user_name[i-1]

Schouldn"t it take last index in the user_name and fit right at the back of the username instead at the front like this : AbeSimpp ?

def username_generator(first_name,last_name):
  return (first_name[0:3] if len(first_name) >= 3 else first_name) + (last_name[0:4] if len(last_name) >= 4 else last_name)

one line!

3 Likes