Python Loop Counter: Track Iterations
In Python, you can utilize a counter variable to keep track of the number of iterations in a loop. For instance, you can increment the counter variable within the loop by 1 each time it loops, and then print out the value of the counter after the loop finishes.
Here is an example code:
count = 0
for i in range(10):
count += 1
print(f"第 {count} 次循环")
print(f"总共循环了 {count} 次")
In this example, we used a variable called count to keep track of the number of loops, and then printed out the current number of loops at the end of each loop. Finally, we printed out the total number of loops.