Python While Loop with if and else with counting on special condition

Viewed 47

Recently I have a python While loop function with if features that require special condition to deal with

import time
from random import randint

while True:

    bid = randint(1,21)
    ask = 20

    #Condition 1
    if ask >= bid:
        print ('Condition 1: ask is bigger!')
        time.sleep (0.1)
        
    #Condition 2
    else:
        print ('Condition 2: bid is bigger!')

    time.sleep(0.1)

Instead of letting this infinite loop running, I would like to add another feature saying if 'Condition 1' occur 10 times in the terminal consecutively, it has to print 'Condition 3: Condition 1 occur 10 times already!'

Anyone have any ideas? Many thanks!

4 Answers

This will check for 10 continuous conditions 1 and break out of the loop

import time
from random import randint


condition1_counter = 0
while True:

    bid = randint(1,21)
    ask = 20

    # Condition 1
    if ask >= bid:
        print ('Condition 1: ask is bigger!')
        condition1_counter += 1
        
    # Condition 2
    else:
        print ('Condition 2: bid is bigger!')
        condition1_counter = 0

    # Condition 3
    if condition1_counter == 10:
        print('Condition 3: Condition 1 occur 10 times already!')
        break

    time.sleep(0.1)

If instead you want to keep the loop going:

import time
from random import randint


condition1_counter = 0
while True:

    bid = randint(1,21)
    ask = 20

    # Condition 1
    if ask >= bid:
        print ('Condition 1: ask is bigger!')
        condition1_counter += 1
        
    # Condition 2
    else:
        print ('Condition 2: bid is bigger!')
        condition1_counter = 0

    # Condition 3
    if condition1_counter == 10:
        print('Condition 3: Condition 1 occur 10 times already!')
        condition1_counter = 0

    time.sleep(0.1)

The below code should help.

import time
from random import randint

bid_count = 0 #stores the number of consecutive bids 
while True:
    bid = randint(1,21)
    ask = 20

    #Condition 1
    if ask >= bid:
        print ('Condition 1: ask is bigger!')
        time.sleep (0.1)
        bid_count = bid_count + 1 #adds 1 to bid counter
    
        #Condition 3
        if bid_count % 10== 0: #determines if the bid is devisable by 10. 
            print('Condition 3: Condition 1 occur 10 times already!')
            #break #breaks loops after condition 3 is met (if needed)
            bid_count = 0 #if you want it to run forever with a reset bid.
        
    
    #Condition 2
    else:
        print ('Condition 2: bid is bigger!')
        bid_count = 0 #Resets bid count to 0.
    


    time.sleep(0.1)

In stead of using while loop you can use for loop for 10 times!

Here's the code will be then,


for i in range(0, 10)
    bid = randint(1,21)
    ask = 20

    #Condition 1
    if ask >= bid:
        print ('Condition 1: ask is bigger!')
        time.sleep (0.1)
    
        condition_1_counter += 1
    
    if if i == 10: # it will run 10 times!
        print("Condition 3: Condition 1 occur 10 times already!")
        
    #Condition 2
    else:
        print ('Condition 2: bid is bigger!')

    time.sleep(0.1)

This should work fine!

import time
from random import randint

check = True
counter = 0
while check:

    bid = randint(1,21)
    ask = 20

    #Condition 1
    if ask >= bid:
        counter += 1
        print ('Condition 1: ask is bigger!')
        time.sleep (0.1)
    #Condition 2
    else:
        print ('Condition 2: bid is bigger!')
        counter = 0
    #Condition 3
    if counter == 10:
        print('Condition 3: Condition 1 occur 10 times already!')
        check = False

    time.sleep(0.1)
Related