FAQ: Introduction to Strings - Review

def username_generator(first_name, last_name):
if len(first_name)<3 and len(last_name)<4:
entire_name = first_name + last_name
return entire_name
elif len(first_name)>3 and len(last_name)>4:
new_username = first_name[:3] + last_name[:4]
return new_username

Why i get ‘None’ to terminal after testing with print(username_generator(“Abe”, “Simpson”))

and with this i get it right?:
def username_generator(first_name, last_name):
if len(first_name)<3 or len(last_name)<4:
entire_name = first_name + last_name
return entire_name
new_username = first_name[:3] + last_name[:4]
return new_username

Since there is no else clause and Abe is three letters, this condition is not met and nothing is returned, hence, None.

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 ?

that’s what I did whoop

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!

Hello. Can anyone suggest more efficient code to solve the problem? Here are the instructions:

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.

def password_generator(username):
  password = ""
  for i in username:
    if not i == username[-1]:
      new_password = password + i + username[0:-1]
      return new_password

Guy on Slack offered this solution. Yeah, not too pleased with myself lol

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

My solution, hope this helps

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

username = username_generator

def password_generator(username):
#Second solution
password = username[1:] + username[0]


#third solution with for loop
password = “”
for i in range(len(username)):
password = password + username[i-1]
return password

When I read the explanation I was like: why? why? is not just easy to sliced. My code is more simple but got the correct answer.

Hi! Pretty new to code and I’m having trouble understanding why I need to use range(0,len(username) in the password_generator function.

Why can’t I just use “for letter in username” like I did in the previous exercise? – Wouldn’t that iterate through every letter already? What is the difference when using range(), and why doesn’t the code run properly if I write it with “in username” instead?

Thanks everyone!

It depends how we wish to access the list. For read-only, we can examine the values, but not change them.

for value in some_list:

If, on the other hand we may wish to access the list for purposes of mutating any one or more values, then we would use range to give us all the indices. By those we can now access the elements and change their values.

for idx in range(len(some_list)):

The value of idx will be an integer, as in the index of one of the elements. We use len() so we get all the indices that are in range for that particular list.

    value = some_list[idx]
    if value == some_value:
        some_list[idx] = value * 2

D’oh! it just hit me, we’re talking strings, here, not lists. Silly me.

That changes everything as far as changing any characters of the string. We can’t, no matter what method we use to access them. However, using range does give us an accurate value for the current index, which can be important, especially when there are repeated characters. Using the read-only access methods, we may get a letter, fine and good, but we can only find the index of the first occurrence of the letter in the string, not any that are subsequent to that.

1 Like