变量分配没有按预期发生。我错过了什么?

Variables assignments are not happening as intended. What am i missing?

我一直在做一个评估棒球统计数据的项目。我正在开发一个模拟完整棒球比赛的程序。对于该程序,我创建了一个名为 at-bat 的函数,它接收球员的上垒百分比并确定球员是上垒还是下垒。我创建了一个 while 循环,表示 while outs 不等于 27,继续执行程序。然而,大多数时候,程序会陷入无限循环,完全忽略 while 条件。我相信变量赋值应该是什么和它实际是什么之间存在一些脱节,但是,我不确定我遗漏了什么。任何反馈将不胜感激。

import random

first= {"obp": 383, "avg": 310}
second= {"obp": 370, "avg": 295}
third= {"obp": 465, "avg": 313}
fourth= {"obp": 351, "avg": 261}
fifth= {"obp": 281, "avg": 221}
sixth= {"obp": 314, "avg": 210}
seventh= {"obp": 297, "avg": 262}
eighth= {"obp": 337, "avg" : 269}
ninth= {"obp": 317, "avg": 277}
order = [first, second, third, fourth, fifth, sixth, seventh, eighth, ninth]

outs=0
total_runs=0
on_base=0
game_log=[]




def at_bat(player):
 x=random.randint(1,1000)
 if player["obp"] >= x:
     print("ON BASE: " + str(on_base))
     return "on_base"
 else:
     print("OUTS: " + str(outs))
     return "out"
     



while outs != 27:
 for a in order:
     if at_bat(a)=="on_base":
         on_base+=1
     else:
         outs+=1



print("Outs = " + str(outs))
print("ON base = " + str(on_base))```

循环遍历 order 中的每个 a,然后在遍历整个列表 后检查它是否不等于输出 。因为 outs 有可能在 for 循环期间递增两次,所以当 outs 跳过 27
时会进入无限循环 如果想outs一到27就停止,不管order中的所有项是否都遍历完,可以这样做

i = 0
while outs != 27:
  a = outs[i]
  if at_bat(a)=="on_base":
    on_base+=1
  else:
    outs+=1

  i += 1
  # Return to the first item
  if i == len(outs):
    i = 0