Osamah Ahmad Osamah Ahmad - 4 months ago 13
Python Question

How do I represent this FOR loop in Python?

How do I represent this structured English 'code' in Python code?

Structured English:


FOR NoOfGamesPlayed <-- 1 TO NoOfGamesInMatch Do


I can do it in VB.NET:

For NoOfGamesPlayed = 1 To NoOfGamesInMatch


I'm confused on how to do it in Python.

Answer
for NoOfGamesPlayed in range(1, NoOfGamesInMatch+1):
    # do stuff

But note that Python uses 0-based indexing, not 1-based indexing.

So for a list of 5 elements, the valid indices are [0] through [4].

Comments