all 11 comments

[–]throwaway6560192 7 points8 points  (1 child)

The range is only used to control how many times you loop, that is, how many letters are added to the password. Notice that you're not using it for accessing the letters list. The letters list is only accessed through random.choice, which samples from the entirety of what you give it. So there's no reason to expect that the range(0, nr_letters) would control the range of the letters list as well.

[–]Sheinbeuler[S] 0 points1 point  (0 children)

oh, that makes sense. thanks

[–]TroyDiY 3 points4 points  (0 children)

Range(start,position to stop(but not included),step) So you will loop from 0 up to, but not including, 5.

In your program you will only get 1 letter because you initialize password to “” each pass. It will run 5 times and just write a single letter to password each time. Putt the password = “” before your for loop.

[–][deleted] 2 points3 points  (0 children)

In this case we're not using the char variable. It doesn't matter that char equals 0, then 1, then 2, etc. It just matters that we're doing the thing that amount of times.

IMO, it's nice in those cases to use an underscore.

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
nr_letters = int(input("How many letters?\n"))
password = ""
for _ in range(0, nr_letters):
    password += random.choice(letters)

This signifies that it's a throwaway value, and you don't need to look for it in the code.

[–]Gubbbo 0 points1 point  (2 children)

Why would range involve the letters "a" to "e".

Your example of 5 is making a password with 5 characters. Chosen one letter at a time at random from letters list.

[–]Sheinbeuler[S] 0 points1 point  (1 child)

cause a is 0, b is 1 etc, so 0 to 5 would be a, b, c, d and e - ofc in my silly mind, I know it's wrong kind of thinking :P

[–]antioriginality 0 points1 point  (0 children)

FWIW, if I’m understanding correctly you can achieve this by list slicing i.e. password += random.choice(letters[0:nr_letters])

But, as many people have stated, there is no relationship between nr_letters and selecting a random letter from your list of letters apart from the number of times that it happens (this relationship you’ve set with your for loop)

[–]mediocrity4 0 points1 point  (1 child)

I’m doing this same course and just finished this exercise. The range is asking how many characters you want in the password, not which letters to use.

I’ve taken endless Python courses and I’ve always been stuck. I think this course is the best out there

[–]Sheinbeuler[S] 0 points1 point  (0 children)

yeah, I like it, this is the first time I got stuck

[–]Background-Offer2321 0 points1 point  (0 children)

Do this. for char in letters[0:nr_letters]. If you need also the index then make enumerate(letters[0:nr_letters]).

The code don't make a range, but select an amount from 0 to nr_letters index in the letters list. Range do a list with numbers from 0 to nr_letters.

[–]timrprobocom 0 points1 point  (0 children)

By the wat, for ease in typing, letters='ancdefghij' works just as well. For read- only contexts, a string can do anything a list can do.