Home > Blockchain >  How to excute statement each time a for loop increase to a certain index in Python?
How to excute statement each time a for loop increase to a certain index in Python?

Time:01-13

Question, count is increamenting, how do i excute a statment each time count adds 2 or 5. Assuming i'm working with a large list of about 1000 or more entries and i want to excute a different statment each time count gets 10, how do i do that?

count = 0
for x in range(10):
    count  = 1
    if count == 2:
        print(f'count is now {count}')

CodePudding user response:

I think that the question is a version of the famous FizzBuzz algorithm:

 def fizzbuzz(n):
     for count in range (n):
         if x % 10 == 0:
             print("fizzbuzz")
         elif x % 2 ==0:
             print("fizz")
         elif x % 5 == 0:
             print ("buzz")
         else:
             print (x)

CodePudding user response:

count = 0
for count in range(1000):
    count  = 1
    if count  == 0: #if count is multiple of 10, say hello
        print(f'Hello')
    else: #else count normally
        print(f'count is now {count}')
  •  Tags:  
  • Related