FAQ: Introduction to Strings - Review

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

print(password_generator(“samuel”))

output is lsamue

Hi! I found out this coding also works, but it was really different from the answer. So is my coding okay or are there loopholes in it? Thanks!

Is this solution ok ?

https://gist.github.com/9155540c48f61072835298c4fa7ca16a

My solution:

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

username = username_generator("Abe","Simpson")

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

def password_generator2(username):
  password = ""
  for i in range(len(username)):
    password += username[i-1]
  return password

print(password_generator(username))
print(password_generator2(username))

I was able to understand the code solution up to the part that states:

for i in range(0, len(user_name)):
password += user_name[i-1]

I would like to know: Why are we using a for loop in this case?
What does [i-1] help us accomplish?

How do both of these actions make us solve the problem we were presented.

Thank you very much!

Yeah… they definitely overcomplicate things. I finished this with 4 lines of code…

def username_generator(first_name, last_name):
  return first_name[:3] + last_name[:4]
def password_generator(username):
  return username[-1] + username[:-1]
1 Like

Amen, did the same! Short and sweet.

Looking at the official solution:

if len(first_name) < 3:
    user_name = first_name
else:
    user_name = first_name[0:3]

The length check / if statement is redundant here and totally unecessary - the below is equivalent in functionality:

user_name = first_name[:3]

If the string is less than 3 characters long, the slice operation will simply grab the whole string and not throw an error.

OMG! Thank you for this! I have been trying to figure this out for the last hour! I had nearly everything correct. I was apparently being dumb in thinking I could append to a string variable lol. Thanks again!

def password_generator(username):
password = “”
for i in range(len(username)):
password.append(i)
return password

Trying to make sense of this

def username_generator(first_name, last_name): if len(first_name) < 3: ** user_name = first_name** else: ** user_name = first_name[0:3] ** if len(last_name) < 4: **user_name += last_name** else: **user_name += last_name[0:4]** return user_name

is there a shortcut to input “user_name” this many times? or is that just something that needs to be learned as part of the “language”
For the most part ive just been reading and doing as instructed, I guess with repetition ill grasp the language and its nuances? I feel like im aimlessly following directions and trying too hard to memorize to retain this info.
I had a bit of trouble getting through this section versus the previous ones. Starting to re-think if programming is a skill I can learn/attain. ive been at it for a solid 2 weeks. is this just growing pains or should I have a full understanding of strings and loops?

I see you are using later lessons… here is mine, thought it was pretty similar to yours lol
def username_generator(first_name, last_name):

if len(first_name) > 2 and len(last_name) > 3:

username = first_name[:3] + last_name[:4]

return username

else:

username = first_name + last_name

return username

def password_generator(username):

password = username[-1] + username[0:-1]

return password

Hi everyone,

I had this answer for the last question of the review :

def username_generator(first_name, last_name):
if len(first_name) < 3:
username = first_name
else:
username = first_name[0:3]
if len(last_name) < 4:
username += last_name
else:
username += last_name[0:4]
return username

def password_generator(username):
password = “”
for i in range(0, len(username)):
password += username[i-1]
return password

It never passed, though the solution had the exact same logic with the username being spelled “user_name”. Since there wasn’t any incoherence in what I wrote, is the fact that the solution didn’t pass pure machine silliness or am I not getting something here ?

Since I’m not really advance I guess I’m missing something, but I don’t know what.

My code below produces the correct answers but it won’t let me continue. Is this code wrong?
“”"
def username_generator(first_name, last_name):
if len(first_name) < 3:
first_three = first_name
else:
first_three = first_name[:3]
if len(last_name) < 4:
first_four = last_name
else:
first_four = last_name[:4]
username = first_three + first_four
print(username)

username_generator(“Abe”, “Simpson”)
username_generator(“Mi”, “Son”)
“”"

Did you ever get this working? Your code passes for me, but I will note the double quote characters in your example are not standard double quotes, they’re this:

U+201C : LEFT DOUBLE QUOTATION MARK {double turned comma quotation mark}

as identified by:
https://www.babelstone.co.uk/Unicode/whatisit.html

If you replace with normal double quotes: " " it should be fine. You should have gotten an error to that effect as well.

Try returning the username instead of printing it :slight_smile:

Thank you, I did replace the double quotes and I got the code to work just fine in the workspace.
i’ appreciate the feedback!

Ok, i think my solution is really messy but super proud of myself because i didn’t have to look up solutions before i actually tried coding :heart_eyes:

def username_generator(first_name, last_name): username = "" if len(first_name) < 3 and len(last_name) < 4: username = first_name + last_name elif len(first_name) < 3 and len(last_name) > 4: username = first_name + last_name[:4] elif len(first_name) > 3 and len(last_name) < 4 : username = first_name[:3] + last_name else: username = first_name[:3] + last_name[:4] return username #test = username_generator("Abelia", "Simpson") #print(test) def password_generator(username): password = "" for i in range(len(username[:-1])): password += username[i] return username[-1:] + password #print(password_generator(test))

I don’t have much to add here, but I need to vent a bit.

range() hasn’t been utilized enough elsewhere in the learning process to justify its inclusion here in the hint section. Also the Solution offered is a bit bizarre, given they showed us how to slice lists in this course. Stuff like this just makes me feel unengaged. However, the solutions provided by users here, such as:

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

Make me feel much better about learning code, because it can be simple. But this course just seems much more slapdash and inconsistent than the one before it, muchlike Datacamp’s course.

1 Like

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

2 Likes

Thank you for this super simple solution, it was killing me trying to do it any other way.
One question though.
Could you explain how this portion works exactly? I understand the first “password = username[-1]” slices the last letter, but I for some reason can’t wrap my head around why the second line adds it to the front of the username.

Thanks in advance!

There’s some useful guidance provided about slicing in the docs that’d be helpful (it’s a tutorial so it should be fairly readable- https://docs.python.org/3/tutorial/introduction.html#strings

Slices effectively have defaults, the first would the zero index, the second would be the length of the sequence and the final one would be a step of one. So x[::] is the same as x[0:len(x):1]. The missing indices use the defaults instead.

In that case hopefully username[:-1] becomes clear, if not add in the defaults. and that may help. Still worth a quick look at the docs for the full details and some nice examples :slightly_smiling_face:.

1 Like

I am kinda confused and upset. I used the following code for this review.
def username_generator(first_name, last_name):
username = first_name[:4] + last_name[:4]
return username

Which got to me the same place and was shorter I am confused too why what I created is wrong. I tried it using my name, both examples to test the print. I didn’t any errors when I print using the username_generator() and added any names to the argument. I am lost as to why I would need to type so much if I got to the same place? Is what I came up with problamatic?