Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop with 2 Conditions. (For Loop)

I have a question below.
I have an array for example:

ex_ar=[1,2,3]

and I have an step size, for example:

step=5

I want to create a loop so that it should print each element during the step size. In this example here it should look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1
2
3
1
2
ex_ar=[1,2,3]
step=6
for i in range(step):
    print(sayi[i])

IndexError: list index out of range

>Solution :

Use modulo list size

ex_ar=[1,2,3]
step=5
for i in range(step):
    print(ex_ar[i % len(ex_ar)])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading