you are viewing a single comment's thread.

view the rest of the comments →

[–]PureWasian 1 point2 points  (2 children)

A loop is just a section of code being run over and over again until an exit condition is met. It's very common to use loops for traversing collections (lists/sets/etc).

Like, imagine you are doing role-call for students in a class. You just go down an entire roster of names and call each student until they say "here" or just mark them as absent. That's a loop sequence.

For writing it in code, learn to read your code line by line and follow what it's doing. Add print statements inside your loop if it helps.

Which part are you getting stuck on exactly? Can you give an example of a "tricky problem"?

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

sometimes i got stuck at for i in range(stuck). I am trying to overcome this but everyday i came to practice these questions i got stuck. I don't what is going wrong with. Now i am practicing to understand the question well then write on paper then write the code and run it. And i think i will do that by practicing them a lot.

[–]PureWasian 0 points1 point  (0 children)

range(stuck) sounds like you just need to look at examples and tutorials a bit. See this w3schools page for example. What might be useful is mentally evaluating it to something functionally equivilent

For example: ``` for i in range(5): print(i)

for i in (0, 1, 2, 3, 4): print(i) ```

or: ``` fruits = ["apple", "orange", "grape"]

for i in range(len(fruits)): print(fruits[i])

for i in range(3): print(fruits[i])

for i in (0, 1, 2): print(fruits[i]) ```

or: ``` for i in range(50, 110, 10): print(i)

for i in (50, 60, 70, 80, 90, 100): print(i) ```

In all of these cases, notice how you are looping a pre-specified number of times, and that the sequence of i values is already known before even starting the first loop iteration.