【问题标题】:Python While Loop with if and else with counting on special conditionPython While 循环与 if 和 else 与计数特殊条件
【发布时间】:2022-09-23 22:42:27
【问题描述】:

最近我有一个 python While 循环函数,它带有需要特殊条件来处理的 if 功能

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)

我不想让这个无限循环运行,我想添加另一个功能,如果“条件 1”在终端中连续出现 10 次,它必须打印“条件 3:条件 1 已经出现 10 次!”

有人有想法么?非常感谢!

  • 创建一个从零开始的计数器,在条件 1 上增加它,在条件 2 上将其设置为零,条件 3 是计数器 == 10

标签: python


【解决方案1】:

这将检查 10 个连续条件 1 并跳出循环

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)

【讨论】:

    【解决方案2】:

    添加一个简单的计数器变量应该可以完成这项工作!

    你可以初始化一个变量,比如cond1_count。在条件 1 之后增加它,在条件 2 之后减少它。

    import time
    from random import randint
    
    cond1_count = 0
    while True:
    
        bid = randint(1,21)
        ask = 20
        print(bid, ask)
        
        if cond1_count and cond1_count%10 == 0:
            print('Condition 3: Condition 1 occur 10 times already!')
            time.sleep(0.1) # or break loop if oyu want!
    
        #Condition 1
        if ask >= bid:
            print ('Condition 1: ask is bigger!')
            cond1_count += 1
            time.sleep (0.1)
            
        #Condition 2
        else:
            print ('Condition 2: bid is bigger!')
            cond1_count -= 1
    
        time.sleep(0.1)
    

    【讨论】:

      【解决方案3】:
      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!')
          #Condition 3
          if counter == 10:
              print('Condition 3: Condition 1 occur 10 times already!')
              check = False
      
          time.sleep(0.1)
      

      【讨论】:

        【解决方案4】:

        就像是 :

        import time
        from random import randint
        
        condition_1_counter = 0
        
        while True:
        
            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 condition_1_counter >= 10:
                print("Condition 3: Condition 1 occur 10 times already!")
                
            #Condition 2
            else:
                print ('Condition 2: bid is bigger!')
        
            time.sleep(0.1)
        

        然后,您可以根据需要将其重置为 0。

        【讨论】:

          【解决方案5】:

          除了使用while 循环,您还可以使用for 循环10 次!

          这是代码,然后,


          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)
          

          这应该可以正常工作!

          【讨论】:

          • 他想停在 10连续的条件 1...无论结果如何,这只会运行 10 次
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-22
          • 1970-01-01
          • 1970-01-01
          • 2021-09-16
          • 2015-10-23
          • 2012-06-18
          • 2012-01-06
          相关资源
          最近更新 更多