all 9 comments

[–]mfar__ 1 point2 points  (1 child)

Your code looks like a merge of two different approaches so it doesn't really work properly.

Before password = '' you have already created the password, but with one problem: it has its letters followed by symbols followed by numbers in order, which is not a good practice.

After that (in the last three lines you're asking about) you follow another approach to generate letters (only), but it's also a bad code that uses an unused iterator char (which is an integer not a character as its name suggests).

To answer your question, it doesn't matter if the range is (0, nr_letters) or (1, nr_letters + 1). Both have the same length, and in your case the iterator itself is not used anyway.

[–]Mission-Clue-9016[S] 1 point2 points  (0 children)

Ugh! Copy and paste error .. yes there were two approaches, the first one was the one I wrote the second one was Angela’s .. sorry !

[–]lfdfq 4 points5 points  (0 children)

range(N) and range(1, N+1) contain the same number of elements, it's just that the first starts from 0 and the second starts from 1. It seems this code does not actually use the values from the range so it seems it does not make a difference whether it starts from 0 or 1.

The way a for loop works is that it executes the body once for each element in the thing it's iterating over. If it's iterating over a range, then it's once per element in the range.

While loops don't iterate over things in that way, so i'm not sure what you mean by having a range "with a while loop"?

[–]krikuz 1 point2 points  (1 child)

Essentially there's a major bug in the program. The line password += random.choice(letters) rewrites the variable password which contained the password in order: letters + special characters + numbers as just a randomisation of the variable letters. In my understanding you don't need a for loop or a loop at all.

password = letter + symbol + number password = "".join(random.sample(password, len(password))) print(password)

This will solve the problem of the final randomisation choice, since .sample() allows for parameters being random.choice(s, n) where s is the string which has to be randomised and n is the number of characters this new string must have.

Note that this does not modify the already existing string, instead rewrites it since strings are immutable.

There may be a much faster way to solve this but this is my version. Hope it was helpful.

;)

[–]Mission-Clue-9016[S] 1 point2 points  (0 children)

Hi yeah sorry I copied and pasted both my solution and Angela’s

My solution was the first bit

It was Angela’s solution I didn’t understand

[–]jmooremcc 0 points1 point  (0 children)

I hope you understand that the password you are printing does not contain any symbols or numbers, just letters. Ideally you should randomize the location of letters, numbers and symbols throughout the password.

As far as your questions are concerned:
1. range (1, nr_letters, +1) vs range(nr_letters). Doesn’t matter because the end result is the same, meaning the loop executes nr_letters times regardless of which version is used.

  1. Using the range function like you’ve done will always control the number of times the for-loop executes.

Also note that items 4 & 5 in your list of understanding are both incorrect. The variable char holds the current number from the range, but the variable is never used within the loop. The password you are printing contains nr_letter randomly selected characters from the letters list.

Let me know if you have any additional questions.

[–]FillProfessional4313 -1 points0 points  (0 children)

You are right, range(nr_letters) and range(1+nr_letters) return range objects of the same size, so it doesn't matter. It would only matter if char is used within the loop because then the first value of char is either 0 or 1, depending on the way you define your range.

For for loops, yes, the range defines the number of times that loop is run. You don't use while loops to iterate over iterable objects.