FAQ: Introduction to Strings - Review

This is my first time sharing my code. It was inspired on tera7798120108 reply (1 Jul 19)
Apparently it works. What do you think?

def username_generator(first_name, last_name):
if len(first_name) < 3:
username = first_name + last_name[:4]
elif len(last_name) < 4:
username = first_name[:3] + last_name
else:
username = first_name[:3] + last_name[:4]
return username

print_username = username_generator(“Abe”, “Simpson”)
print(print_username)

#######################################

def password_generator(user_name):
password = user_name[-1:] + user_name[:-1]
return password

print_password = password_generator(print_username)
print(print_password)

Hi guys,

For Question #1, I created a shorter code (comparing to the solution) to solve the problem as follows:
correct

Then I want to make it even shorter by return the variable “username” as follows, but it shows invalid syntax and I don’t know why. Any idea?

very helpful, thanks.
I have been trying to understand the way the explanation, but this makes it so much easier.

Hey,

I managed to finish the first one pretty fine but the last one was really hard for me.
I looked at the solution, and it’s using range() and I don’t get why would we need range() and len(), I personally tried to do

  for letter in username:
     password += username[-1] + username[:-1]
  return password

but it would return pAbesimpAbesimpAbesimpAbesimpAbesimpAbesimpAbesimp,

then I tried to just remove the for loop, and it worked, why does the exercise wants us to use so much things? I mean the purpose of the password generator is just getting the last letter to the front, we don’t need any loops to do this.

here was my final code

def username_generator(first_name, last_name):
  username= ""
  if len(first_name) < 3 and not len(last_name) < 4:
    username = first_name + last_name[:4]
    return username
  elif len(last_name) < 4:
    username = first_name[:3] + last_name
    return username
  else:
    username = first_name[:3] + last_name[:4]
    return username

new_username = username_generator("Abe", "Simpson")

def password_generator(username):
  return username[-1]+username[:-1]

Here is my solution:

def username_generator(first_name, last_name):
    if len(first_name) < 3 or len(last_name) < 4:
        username = first_name+last_name
    else:
        username = first_name[:3]+last_name[:4]
    return username

def password_generator(username):
    password=""
    length=len(username)
    for i in range(length):
        # at each letter, at the previous letter to password
        password += username[i-1] # i-1 mean previous index
    return password

Thanks for the replies in this topic! I got soooo frustrated with the password generator, but I scrapped my code, had a coffee, and thought about it again and got it! I added in a bunch of ‘debug’ print statements to help me along, but my final code was 6 lines.

Here’s how I thought of it, in case this helps anyone else.

Remember how you can get the last item in a list. A string is just a list of letters.

Use that concept as your starting point in a range - A range doesn’t have to be all positive numbers and can contain a 0!

Here’s what I came up with:

def password_generator(username):
  password = ""
  for i in range(-1, len(username)-1):
    password = password + username[i]
  print(password)
  return(password)

Hey, with the second solution doesn’t the letters in the username shift to the left rather than going on the right?

I don’t think so. for example for i = 0, code adds (0-1) = last character from username to it implying that all the characters are shifted to the right. similarly, 0th character is added to position 1 ( as i-1 = 0 for i=1) so that means password’s index 1 is occupied by 0th character from username which is again shift to the right.

I did the same way and came here to check if this is acceptable since we haven’t shifted the letters as asked. I felt like it’s cheating lol

def username_generator(fn, ln): return fn[:3] + ln[:4] def password_generator(un): return un[-1] + un[:-1] your_username = username_generator('Michael', 'Jackson') your_password = password_generator(your_username) print(your_username) print(your_password)

What does

username[i-1]

stand for in this solution:

def password_generator(username):  
  password = ""
  for i in range(len(username)):
    password += username[i-1]
  return password

My answers were not what the course wanted but did work! I am wondering if there was anything that could mess up the methods I used?

def username_generator(first_name, last_name): user_name = first_name[0:3] + last_name[0:4] if len(last_name) < 4 or len(first_name) < 3: user_name = first_name+last_name return user_name def password_generator(user_name): passwordlist = [] password = "" count = 0 for x in user_name: count += 1 passwordlist.append(user_name[count - 2]) for xy in passwordlist: password += xy return password

Thank ya for the help if you spare the time!

I think this will allow the generator to return the final character in the list first.
Or to explain a separate way, username: referring to the string passed to the argument. username[i-1] will give you the index directly before the one it is currently on?
I don’t know if this helps but hope it does!

1 Like

I used this code and it worked for me as well:
def password_generator(username):
letters =
for i in range(len(username)):
letters.append(username[i-1])
password = ‘’.join(letters)
return password

can you help me? what does this mean?:

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.

def username_generator(first_name,last_name): FN_slice = first_name[:3] LN_slice = last_name[:4] if (len(first_name) < 3) and (len(last_name) < 4): user_name = first_name + last_name return user_name else: return FN_slice + LN_slice

I have a general Codecademy question. I recently completed the Python Fundamentals Part I course. After working with DataCamp for about a year, I fell in love with Codecademy because there were no gaps between lessons.

DataCamp, for example, seems to assume a lot of prior knowledge in its beginner courses. Similarly, the practice activities ask students to solve problems for which the students have had no on-platform exposure.

I’ve just begun Python Fundamentals Part II in Codecademy. I’m starting to see this horrible pattern here. That is, Codecademy is making assumptions about knowledge level and/or asking of me some things I haven’t learned.

I’m a diligent learner–a beginner learner–but diligent nonetheless. I’m on a free trial license. I’m unwilling to shell out the cash for a full license if Codecademy is just going to repeat the aspects of DataCamp that I really dislike.

Any thoughts? Does it get easier from here, or am I in for more of the same?

Thanks!

1 Like

Your solution to the username_generator is not correct!

Using your method the name Abe Simpson would display as “AbeSimpson”.
In the question it says:

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

To generate ‘AbeSimp’ then the code should be a little more complex:

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

For both “PART 1” and “PART 2” sections you do not need range or length you simply need to say:

else:
part1 = first_name

AND

else:
part2 = last_name

If the names are less then 3 and 4 in length (respectively) then you are just adding the whole name, hence the above code.

Hope that helps :slight_smile:

The solution uses so many if statements, which feels very literal, but is there any reason why this solution doesn’t work? I’ve testing it with short and long names, seems good. Maybe an older version of Python?

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

After looking at the answer they wanted I’m also doubting how I got an answer that worked. I was unsure how I’d apply range to this so just applied a standard temp value ‘letter’ and len(). But now I feel like I’ve confused myself, is anyone able to explain why this works without needing range, please?

def password_generator(user_name): 
  password = user_name[-1:] + user_name[:-1]
  for letter in user_name:
    user_name[len(user_name)-1:]
  return password

After looking at explanations for how to do it with range it makes more sense but I’m still curious about this, thanks for any help you can give.