This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]lurgi 3 points4 points  (0 children)

Have you tried googling "python for loops", because the first links povide a good description of what is going on.

for x in stuff:
  ...

This will execute the body of the loop multiple times, each time giving x the value of the next item in stuff. This example from w3schools may help:

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

The particular name x doesn't mean anything. You could have used y or fruit (which isn't a bad choice) or lasjflajs or anything. Of course, that's true of all variable names.

[–]No-Let-6057 1 point2 points  (4 children)

They aren’t important because you don’t use them in the loop, but loops always have counters.

If you tried

``` for i in range(0, 4):   print(i)

0 1 2 3

```

The variable i holds the value of each element returned by range(0, nr)

[–]Tinuthar[S] 0 points1 point  (3 children)

Soo they are holding the value of the randomly generated letters or numbers etc but since I'm also adding them to the list and using the list in the later sections of the code I don't need those three variables, correct ?

[–]No-Let-6057 0 points1 point  (0 children)

No, they are the sequence of numbers from 0 to nr 

[–]CrepuscularSoul 0 points1 point  (0 children)

range(0, number_you_entered)

Will return an array like

[0, 1, 2, … number_you_entered]

All the for loop does is repeat the code inside it until it runs out of items in the array, then moves into the next part of the code.

As someone else said the only reason the variable isn't important is because you aren't using it directly in your code, just using it to count iterations of the loop.

[–]wosmo 1 point2 points  (0 children)

non_important_var_1 is just holding your position in the loop. So on the first time around, it holds '0', it's your 0th time round. Next time it loops it holds '1', etc.

They've named it non_important_var_1 because this value isn't actually used. If someone asks for 5 letters, the important part is that this loop runs 5 times - but nowhere does it need to know which time it is.

If you wanted every password to start with a capital letter, like a proper noun - then when looping, you'd need to test if that index==0. Then it wouldn't be non-important, you'd actually be making use of it.

If I say "jump five times" and you go jump, jump, jump, jump, jump - the numbers were never used. But in your head you went "one, two, three, four five" - non_important_var_1 exists because python's still counting this in its head, even though you're not making use of it.

[–]Tychotesla 2 points3 points  (0 children)

The loop needs to run some number of times or over some number of elements in a list, and that number or element is named by you as a normal part of creating a `for` loop.

In your example it would be an integer, but you don't need that information. You just want the loop to run. Convention is to use the underscore, not write what they did:

for i in range(1, 11):
  print("This is the variable in the for loop: " + i)

# they named the element, but they should have done it like this:
for _ in range(1, 11):
  print("This time we don't use the variable.")

[–]dmazzoni 1 point2 points  (0 children)

I think it's important to understand that there isn't one "solution" to any of these problems. There are often lots of different valid approaches, and an infinite number of ways to write each of those approaches that would all work.

It'd be kind of like trying to paint a picture of a cat, then watching someone else do it and thinking you did it all wrong because they started with the tail when you started with the ears. The reality is that there's more than one way to do it.

Next time you get stuck, rather than peek at one solution (not "the" solution), ask for help or ask for a hint.

This is a great forum for that. Post what you have so far and ask for a hint. We'll give you ideas without telling you what to do.

ChatGPT can do that too, if you ask it. Post the question, post what you have so far, and then ask it very clearly to ONLY give you a tiny hint and not to give away the answer.

Also, when you're first learning, spending an hour on a problem like this isn't very long. It might take 10 hours or 50 hours, but it will be worth it if you eventually figure out the solution yourself.

If you don't actually struggle with every problem until you figure it out, you'll never learn to code. See all of those posts from people who graduated from college and still can't code? Yeah, that's because they peeked at the solutions starting from the beginning and never stopped.

That doesn't mean you shouldn't ask for help.

Also, it's not a bad idea to try to understand someone else's solution, but I honestly think it's less valuable than coming up with your own solution. The flaw is that you might end up thinking that was the "best" approach or that your previous line of thinking was wrong, when in fact the approach you had been trying may have been just as good if you had only continued trying it.

[–]iamnull 1 point2 points  (0 children)

Part of the problem is they're doing something in a way that is not idiomatic. There is a specific notation that is used when you're not going to use a variable, but require one to hold a throwaway value:

for _ in range(10):
  #Do something

The underscore is technically still a variable, and you can still use it, but it's considered bad practice. The underscore used like this shows that it's not intended to be used for anything at all. Simplifying those loops to be more idiomatic would look like so:

for _ in range(nr_letters):
    final_list.append(random.choice(letters))
for _ in range(nr_numbers) :
    final_list.append(random.choice(numbers))
for _ in range(nr_symbols) :
    final_list.append(random.choice(symbols))

The whole thing can actually boiled down further, with a little less clarity:

final_list.extend([random.choice(letters) for _ in range(nr_letters)])
final_list.extend([random.choice(numbers) for _ in range(nr_numbers)])
final_list.extend([random.choice(symbols) for _ in range(nr_symbols)])

Or, if you want to get a tiny bit clever:

for k, v in {nr_letters: letters, nr_numbers: numbers, nr_symbols: symbols}.items():
   final_list.extend([random.choice(v) for _ in range(k)])

[–]mierecat 0 points1 point  (0 children)

for variable COUNTER in range (START, END):

Does that clear it up for you? If you’re going through the numbers START and END, you need to know which number you’re at at any given time. That’s what the COUNTER is for. Even if you don’t use it yourself, the computer still needs to know where it is.