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]
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
username = first_name[:3]+last_name[:4]
for i in range(length):
# at each letter, at the previous letter to password
password += username[i-1] # i-1 mean previous index
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:
password = ""
for i in range(-1, len(username)-1):
password = password + username[i]
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!
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?