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.
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)
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 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!
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
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.
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?
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?
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.