How to log the number of times a loop is executed in Python?
One option is to use a counter variable to keep track of the number of loops, and increment the counter variable by 1 after each loop iteration. For example:
count = 0
while condition:
# 循环操作
count += 1
print("循环次数:", count)
Alternatively, you can use the built-in function enumerate() in a for loop to keep track of the number of iterations. For example:
for count, item in enumerate(sequence):
# 循环操作
print("循环次数:", count+1) # count从0开始计数,所以最后需要加1