16 posts were split to a new topic: Is it necessary to use a loop?
My solution, hope this helps
def username_generator(first_name, last_name):
if(len(first_name) < 3 or len(last_name) < 4):
return first_name + last_name
else:
return first_name[:3] + last_name[:4]username = username_generator
def password_generator(username):
#Second solution
password = username[1:] + username[0]####################################
#third solution with for loop
password = “”
for i in range(len(username)):
password = password + username[i-1]
return password
Chipping away:
def username_generator(fname, lname):
return fname[:3] + lname[:4]
def password_generator(username):
password = ""
for i in range(len(username)):
password += username[i-1]
return password
I’m confused about when to use range() and when to use len() in these exercises. Can someone explain why this is wrong, for the first step in the exercise? I’m reasonably sure it has something to do with the steps where I use “range” but I don’t know why or how to fix it.
Also, can anyone tell me how to indent, in these replies/questions? Not being able to indent is making me crazy.
def username_generator(first_name,last_name):
if len(first_name) > 3:
part1 = first_name[:3]
else:
part1 = range(len(first_name))
if len(last_name) > 4:
part2 = last_name[:4]
else:
part2 = range(len(first_name))
username = part1 + part2
return username
The way this exercise wants you to complete it is quite complicated. I was able to write both functions using only 6 lines of code. In my code I’ve used f-strings (which haven’t been introduced yet), but it could be accomplished just as easily with string concatenation.
def username_generator(first_name, last_name):
username = (f"{first_name[:3]}{last_name[:4]}")
return username
def password_generator(username):
password = (f"{username[-1]}{username[0:len(username) - 1]}")
return password
The exercise says to implement password_generator
using a loop, but slicing is much cleaner!
def password_generator(username):
return username[-1] + username[:-1]
Why do the first pair of print statements work and second not work?
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
def password_generator(user_name):
password = ""
for i in range(0, len(user_name)):
password += user_name[i-1]
return password
print(username_generator("Joe","Smith"))
print(password_generator(username_generator("Joe","Smith")))
username_generator("Joe","Smith")
print(user_name)
password_generator(username_generator("Joe","Smith"))
print(password)
I get the following error for the second pair of print statements:
NameError: name ‘user_name’ is not defined
(and the same error for ‘password’)
user_name can only be accessed inside the function username_generator which is local scope unless you declared a variable user_name in the global scope which is outside of the functions!
If we do not want to use a loop is this solution viable?
def password_generator(username):
return username[-1]+username[0:-1]
Hi! I found your solutions very helpful! However, I didn’t quite understand how the password_generator()
works and output the letters shifted to the right. Could you please explain a bit? Thanks!
Since the loop is “i in range(length of username)”, the very first i would be 0. The goal is to use the last letter as the first letter, then the rest of the string in order.
So, the first iteration is i - 1, or 0 - 1, which is -1. The value -1 points to the last value of the index of a string or array. If I had the name “Steve” it would target the last “e” first. Then, we add 1 to i as the next part of the loop, making the new value 1 - 1, or 0, which is the S. This will loop until it finishes with eStev. It stops after 5 loops because len(“Steve”) = 5.
Thanks for the explanation! Much clear now. So I can deduce that password = ""
will only contain 5 letters because len(“Steve”) = 5 and that’s when the loop stops, right? giving the "eStev"
output.
“Go Enjoy”
Because len(“Steve”) = 5 is used with range you end up with range(len(“Steve”)). So range with no start will start with 0 and go to 5 because of the length of “Steve”. It’s about the same as using range(0, 5).
Hey,
I’m struggling to understand when we should use range to iterate through indices rather than the normal method.
I did this exercise like that:
> def password_generator(user_name):
>
> pass = ''
>
> for char in user_name:
>
> pass += user_name[char-1]
>
> return pass
And the correct way was this:
> def password_generator(user_name):
>
> pass = ''
>
> for char in range(0, len(user_name)):
>
> pass += user_name[char-1]
>
> return pass
I thought that
> "for i in string"
would iterate throughout the length of that string? When do we need to use “range” and “len” instead of the normal method?
Sorry for the beginner question.
They are both very different. The first example would not work because its going to take the user_name that is passed to the function password_generator say “Lucas”. The for loop is going to iterate through the string “Lucas” starting with the first pass and put L in the temporary variable char.
At this point its fine but when it goes to use the temporary variable char in the next statement you can’t do L - 1 because L is a string and 1 is an integer. This is going to give you a TypeError.
In the second example the for loop is using all integers and will not have that issue. First you find the length of your user_name string that is passed to the function and using that number with range you can create a range of numbers to use each time though the for loop.
So if your user_name string that was passed into the function had a length of 6 you would use range and the first time char would be 0 than 1 and 2 and so on until you get to the length of your user_name string. Doing this and using the number in the next statement you can now run char - 1 because you will not get an error running 0 - 1 than 1 - 1 and so on. This number will be used for your user_name index location and will store the information from that index in pass each time.
You can test this from adding print(char) following the for loop to watch what happens to char each time through the loop.
The last thing you want to watch out for is using the word pass as your variable name. Python has something called a pass statement so the key word is already defined in python and can’t be used.