If anyone is struggling with the second part in generating a password, here is the solution and line by line explanation:
def password_generator(username):
password = ''
for i in range(len(username)):
password += username[i - 1]
return password
Line 1: Define function with the parameter of username (With the purpose of inputting a string).
Line 2: Create an empty string and store it in a variable called password. This was explicitly stated in the problem prompt itself. After all we want to, “add the previous letter to the currently empty string called password.” We are preparing to add characters from String A (username) to String B (password).
Lines 3 & 4: Create a for loop with the intent of iterating through each index of String A (username). Every successive loop results in adding the previous letter to the empty string called password, which was created in line 2.
for i in range(len(username)):
password += username[i - 1]
The above for loop uses range because we want to access each character via an index. For instance if username = “rob” the code will flow like this:
for i in range(3):
password += "rob"[0 - 1]
We get a value of 3 inside range because the length of the string “rob” is 3. And so we get a range of (0, 1, 2) that our i variable, defined in the for loop, is equal to.
In the first loop we perform this action:
for i in range(3):
password += "rob"[-1]
At this stage we added the character b to our String B (password) because "rob"[-1]
== “b”. Remember that our initial i value is set to 0 which is how we got "rob"[-1]
because 0 - 1 = -1. We subtract a 1 from our i value every time we loop because doing so ensures that we, “shift all of the letters by one to the right, so the last letter of the username ends up as the first letter.” We continue this process until the loop finishes. But for reference …
Loop 1: We add the string “b” to the password string via "rob"[-1]
Loop 2: We add the string “r” to the password string via "rob"[0]
Loop 3: We add the string “o” to the password string via "rob"[1]
Line 5: Return the now completed password string. When we call this function we should get a string value of “bro”. Notice that the return is outside of the loop because we want the loop to finish before we return the string.
Sorry if my explanation is too wordy! I saw other solutions in this post from various users but I think this is the easiest to read and understand for me. It took me a little while to figure this one out but it’s not complicated. If you had a difficult time in figuring out the solution, please don’t be discouraged. Instead I would remember these 4 things:
-
Make sure you understand what the problem is asking for you to implement. This is the vital initial step. Take the time to understand the problem before you start coding.
-
Partition or solve the problem in chunks. If you know for sure that you need a for loop in your code then just type it in so you have an easier time visualizing what you’re doing.
-
If you don’t understand a fundamental programming concept like functions, loops, etc or something ancillary like the range function in a for loop, please review those concepts to get a good grasp before moving on. A project requires tools in a toolbox, and these concepts are like tools.
-
Don’t be discouraged if you have to use Google or take a peek (after trying on your own) at the solution. I wouldn’t consider it cheating unless you just copy + paste and proceed onto the next exercise without understanding the code. That’s not really possible with Google anyways, you still have to most likely modify what is pasted, which implies that you know something of what is going on. I also wouldn’t take it as a sign of lesser ability because such thinking serves no purpose in helping you succeed in the moment!
Cheers and happy coding.