Is it necessary to use a loop?

In second part why we need loop if we can get answer trough slicing.

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

this exercise can be completed without any looping, len(), or range.

for the first function

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

this works, no need to specify a string length for this task

and for the second function

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

I wish they had designed the problem so it needed loops and other python capabilities


Yeah. I agree with you.

I agree with you.
My code is as same as yours.:handshake:

you could even simplify that more if you wanted.

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

But I think the purpose of using a for loop is to make sure you can use the whole content provided in this chapter instead of a slice of it.


As for the username_generator function:
This works, but it looks a little stilted. Is there a more “elegant” way to write this?

Code example:
def username_generator(first_name, last_name):
username = “”
slc1 = “”
slc2 = “”
if len(first_name)<3:
slc1 = first_name
slc1 = first_name[0:3]
if len(last_name)<4:
slc2 = last_name
slc2 = last_name[0:4]
username = slc1 + slc2
return username

Edit: i meant, while still using loops?

I’m really confused on the Password Generator function. Is there a basic explanation on how this would work? I think this has made me realise my knowledge of loops may not be strong enough to progress through the course at the moment but i’d really appreciate an explanation on how a loop could be used to produce the correct output


The only use for a loop that I can see is in the password generator, as opposed to using slices. It is how we take,


and generate,

1 Like

Yes, this is what I used, and although it’s simpler, I understand why Codecademy wanted to use a for loop to show how to iterate through strings. I’m still happy it accepted both answers though.

Is there a reason you wouldn’t use my solution? I can’t figure out why they would want you to use a loop here unless it’s just about practicing the concept. I get both solutions, I’m just curious if there’s a reason you would not want to use this solution.

def password_generator(username):
    password = username[-1] + username[0:(len(username) - 1)]
    return password

that’s what I did whoop

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]

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

Agreed; I also used username[-1] + username[:-1] and came to the forum with the same question in mind. I get that they want to review all the topics from the section, but I would like to see a question designed to do that more purposefully.

Saying to loop from 0 to len(username) is especially confusing since Python doesn’t have for loops in the for(i = x; i < y; i++) style.

for x in range(len(username)):

pretty much does it. That and your example both give an index on each iteration.

True; I did forget about range.

I know the benefits of using range vs looping through with for…in… vs using string[i] are probably situational. I’m curious, in this example, do you think there a reason to use range other than that they asked us to?

If you need to access the index, use range to create the sequence of indices. If you only want to poll the value (and not change it) then use the read-only loop.

for x in iterable:    #  read-only

for i in range(len(iterable)):    #  index sequence