Home python Python. Loop counter increment

Python. Loop counter increment

Author

Date

Category

How to increment a counter while looping

for param in range (len (mass)):
  if a == b:
  ...
  ...
  param + = 5
  else:
  ...

This could have been done manually in C #

for (i = 0; i & lt; val; i ++)
{
if (a == b)
{
...
i + = 5;
}
else ...
}

But it doesn’t work in python, the counter keeps going no matter what.


Answer 1, authority 100%

Replace the for loop with while :

param = 0
while param & lt; len (mass):
  if a == b:
    ...
    ...
    param + = 5
  else:
    ...
  param + = 1

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions