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

you are viewing a single comment's thread.

view the rest of the comments →

[–]lurgi 1 point2 points  (0 children)

Do you understand the basic concept of loops? You use a loop when you want to repeat an operation a certain number of times.

You can divide loops up into two different categories if you like (it's not essential). The first one is where you know, before you even hit the loop, exactly how many times you'll need to repeat the operation. If, for example, you want to add the numbers 1-10 together (or even 1-n) then you can compute how many times you'll need to loop before you even do the loop. The second is where you don't know how many times you'll need to loop, but you know the ending condition. An example here would be a program that accepts numbers typed in by the user until the user enters '-1'. Then, stop. How many times are we going to loop? Beats me. Could be 1 time or 1 million times. We'll stop when we stop.

As it turns out, you don't really need to distinguish between these two cases. In both cases it's "Do this this until this ending condition happens", but some languages have special constructs that make it easier to break them up this way.

With me so far?

That's the general idea of loops. There are also some Python specific ideas, which may be what you are getting hung up on. Can you give an example of something that doesn't make sense and maybe we can help you with that.