looking for some solutions? You are welcome.

SOLVED: Why does the position of this program affect the rest of my code?

Mr. Dribbles:

I'm creating a Python program that simulates a baseball game using stats from real players.

I noticed that when I put set_bases() before my bases_advance(x) program, the game starts in the middle and it does not even print "play ball". The print("play ball") thing is in the outermost indentation level, so I can't imagine how it could depend on anything else.

from random import randint

player=[]
bb_random=[]
hit_random=[]
player_position=[]
#below is index for player up
i=0
inning=0

pa=0
ab=1
bb=2
h=3
b2=4
b3=5
hr=6
so=7
sb=8
avg=9
obp=10

inning_list=["Top of the 1st","Bottom of the 1st","Top of the 2nd","Bottom of the 2nd","Top of the 3rd","Bottom of the 3rd","Top of the 4th","Bottom of the 4th","Top of the 5th","Bottom of the 5th","Top of the 6th","Bottom of the 6th","Top of the 7th","Bottom of the 7th","Top of the 8th","Bottom of the 8th","Top of the 9th","Bottom of the 9th","Top of the 10th","Bottom of the 10th","Top of the 11th","Bottom of the 11th","Top of the 12th","Bottom of the 12th"]

players = {"yelich" : [111.,33],
"cain": [12, 24], "pina": [123, 50 00,]}

#code to print all player stats
for player, play_stats in players.items():
  print(print, ",".join(player_stats))

#stats team 1=(pa, ab, bb, h, b2, b3, hr, so, sb, avg, obp)
team_names="the Brewers","the Cubs"
#yelich should be 326 avg and cain 308
yelich=[651, 574, 68,187,34,7,36,135,22,326,402]
cain=[620, 539, 71, 166, 25, 2, 10, 94, 30, 308, 395]
shaw=[587, 498, 78, 120, 23, 0, 32, 108, 5, 241, 345]
aguilar=[566, 492, 58, 135, 25, 0, 35, 143, 0, 274, 352]
braun=[447, 405, 34, 103, 25, 1, 20, 85, 11, 254, 469]
arcia=[366, 348, 15, 103, 25, 1, 20, 87, 7, 236, 268]
perez=[334, 316, 17, 103, 11, 2, 9, 71, 11, 253, 290]
pina=[337, 306, 21, 77, 13, 2, 9, 62, 2, 252, 307]
villar=[279, 257, 19, 67, 10, 1, 6, 80, 14, 261, 315]

#stats team 2=(pa, ab, bb, h, b2, b3, hr, so, sb, avg, obp)
baez=[645, 606, 29, 176, 40, 9, 34, 167, 21, 290, 881]
rizzo=[665, 566, 70, 160, 29, 1, 25, 80,6, 283, 376]
contreras=[554, 474, 53, 118, 27, 5, 10, 121, 4, 249, 339]
zobrist=[520, 455, 55, 139, 28, 3, 9, 60, 3, 305, 3780]
almora=[479, 444, 24, 127, 24, 1, 5, 83, 1, 286, 323]
heyward=[489, 440, 42, 119, 23, 4, 8, 60 ,1, 270, 335]
schwarber=[510, 428, 78, 102, 14, 3, 26, 140, 4, 238, 356]
russell=[465, 420, 40, 105, 21, 1, 5, 99, 4, 250, 317]
bryant=[457, 389, 48, 106, 28, 3, 13, 107, 2, 272, 374]

player_lineup=[yelich,cain,shaw,aguilar,braun,arcia,perez,pina,villar]
player_lineup_opponent=[baez, rizzo, contreras, zobrist, almora, heyward, schwarber, russell, bryant]
player_lineup_label=["yelich","cain","shaw","aguilar","braun","arcia","perez","pina","villar"]
player_lineup_label_opponent=["baez","rizzo","contreras","zobrist","almora","heyward","schwarber","russell", "bryant"]
both_team_lineups=[player_lineup,player_lineup_opponent]
both_team_lineup_labels=[player_lineup_label,player_lineup_label_opponent]
#print(both_team_lineups[1][1][0])

#both_team_lineups[which team is up][which player is up][which stat is used]
#so you can do both_team_lineups[team_up][i][stat]

all_player_bounds=[]
runs=0
player_base_pos=[0,0,0,0,0,0,0,0,0]

for i in range (100):
  bb_random.append(randint(1,1000))
  hit_random.append(randint(1,1000))
  #player_position.append(i)

for i in range(9):
  all_player_bounds.append([0])

for i in range (9):
  all_player_bounds[i][0]=player_lineup[i][b2],player_lineup[i][b2]+player_lineup[i][b3],player_lineup[i][b2]+player_lineup[i][b3]+player_lineup[i][hr],player_lineup[i][ab]
  #print(all_player_bounds[i])

#print that big list for hit chance boundaries
#print(all_player_bounds[0][0][4],"\n\n\n")

#BASE ADVANCEMENT BEGINS=======================================================================================
single=1
double=2
triple=3
home_run=4
whose_on_first=[0,0,0]
player_pos_1=[0,0,0,0,0,0,0,0,0]
player_pos_2=[0,0,0,0,0,0,0,0,0]
player_pos=[player_pos_1, player_pos_2]
runners_on_base=0
team_up=0
runs=[0,0]
batter_up=0
runs_this_inning=0
hit_type_list=["none","single","double","triple","home run"]

print(player_pos[team_up])

#OLD FUNCTION
#def set_bases():
  #for d in range (0,4):
    #print("before we set bases, they are ",whose_on_first)
    #print("d is ",d)
    #if d in player_pos[team_up] and d!=0:
      #whose_on_first[d-1]=1
      #print("whose on first was attempted")
    #elif d not in player_pos:
      #whose_on_first[d-1]=0
    #print(player_pos)
    #print("bases are now ",whose_on_first,".\n")

def set_bases():
  if 3 in player_pos[team_up]:
    whose_on_first[2]=1
  else:
    whose_on_first[2]=0
  if 2 in player_pos[team_up]:
    whose_on_first[1]=1
  else:
    whose_on_first[1]=0
  if 1 in player_pos[team_up]:
    whose_on_first[0]=1
  else:
    whose_on_first[1]=0

set_bases()

#OLD FUNCTION THAT DESN'T WORK
#def bases_advance(x):
  #print("running base advancement for a ",hit_type_list[x])
  #for d in range(4):
   # global team_up
   # global batter_up
   # global i
   # global runs_this_inning
    #print("checking for player ",d)
    #print("d is ", d)
    #print("bases should advance")
    #if player_pos[team_up][d]!=0:
    # if x >= (4-player_pos[team_up][d]):
       #print("path 1")
   #    player_pos[team_up][d]=0
    #   global runs
     #  runs_this_inning=runs_this_inning+1
      # runs[team_up]=runs[team_up]+1
       #if runs_this_inning==1:
        # print("{} gets a run!".format(team_names[team_up]))
       #if runs_this_inning>1:
        # print("{} gets another run!".format(team_names[team_up].runs_this_inning))
     #else:
      #print ("path 2")
      #print("player_pos is ",player_pos)
      #player_pos[team_up][i]=player_pos[team_up][i]+1
  #player_pos[team_up][batter_up]=x
  #print("player_pos is ",player_pos)
  #set_bases()

#NEW AND EPIC FUNCTION
def bases_advance(number_of_bases):
  #print("at the start, player_pos is ",player_pos[team_up])
  for f in range (0,8):
    #print("VARIABLE F is ",f)
    for g in range (1,4):
     #print("g is ",g)
     if g==player_pos[team_up][f]:
      player_pos[team_up][f]=player_pos[team_up][f]+number_of_bases
    #global i
    #print(player_pos[team_up])
  player_pos[team_up][i]=number_of_bases
  #print(player_pos[team_up])
  #for RUNS
  for f in range (0,9):
    if player_pos[team_up][f]>=4:
      global runs
      runs[team_up]=runs[team_up]+1
      #print("runs are ",runs)
      player_pos[team_up][f]=0
      #print(player_pos[team_up])
  set_bases()


#print(player_pos)
#set_bases()
#print("\nbases are ",whose_on_first)
#ases_advance(1)
#print(player_pos)
#print("bases are ",whose_on_first)
#print("runs =",runs,"\n\n")
#bases_advance(2)
#print(player_pos)
#print("bases are ",whose_on_first)
#print("runs =",runs,"\n\n")
#bases_advance(1)
#print(player_pos)
#print("bases are ",whose_on_first)
#print("runs =",runs,"\n\n")
#bases_advance(4)
#print(player_pos)
#print("bases are ",whose_on_first)
#print("runs =",runs,"\n\n")

#BASE ADVANCEMENT BEGINS=======================================================================================

#both_team_lineups[which team is up][which player is up][which stat is used]
#so you can do both_team_lineups[team_up][i][stat]

#print(both_team_lineup_labels[1][0])

print("= = = = = = =\nPLAY BALL!\n= = = = = = =\n\n")
i=0
i2=0
batter_up=i
outs=0
team_up=0
randindex=0

#RUN INNINGS FUNCTION

def run_inning():
  global outs
  global team_up
  global team_up
  global randindex
  global batter_up
  global i
  global inning
  global whose_on_first
  inning_index=[0,0]
  fix_skip_error=0
  win=0
  while outs <3:

    #if outs!=0:
      #print("team up is ",team_up," so we should have,", both_team_lineup_labels[team_up][i])
      #print("This is a test of whose on first. whose on first is ",whose_on_first, whose_on_first[0], whose_on_first[1], whose_on_first[2])

    #if 1==1:
      #print("i is",i)
      #print("randinex is", randindex)
      #print("bb is",bb)
      #print("bb_random[randindex is",bb_random[randindex])
      #print("bothteamlineups team up i pa is ",both_team_lineups[team_up][i][pa],"\n")
    print("\n     ",whose_on_first[1],"\n\n",whose_on_first[2],"       ",whose_on_first[0],"\n\n")
    if bb_random[randindex]/1000< both_team_lineups[team_up][i][bb]/both_team_lineups[team_up][i][pa]:
      print("{} gets a walk".format(both_team_lineup_labels[team_up][i]))
      bases_advance(1)
    else:
      if hit_random[randindex]<=both_team_lineups[team_up][i][avg]:
        hit_type=randint(1,both_team_lineups[team_up][i][ab])
        if 1<=hit_type<=all_player_bounds[i][0][0]:
          player_base_pos[i]=2
          print("{} gets a DOUBLE!".format(both_team_lineup_labels[team_up][i]))
          bases_advance(2)
        elif all_player_bounds[i][0][0]<hit_type<=all_player_bounds[i][0][1]:
          player_base_pos[i]=3
          print("A very rare TRIPLE from {}".format(both_team_lineup_labels[team_up][i]))
          bases_advance(3)
        elif all_player_bounds[i][0][1]<hit_type<=all_player_bounds[i][0][2]:
          #player_position[i]=4
          print("{} hits a HOME RUUUUUUUN!!!!!!".format(both_team_lineup_labels[team_up][i]))
          bases_advance(4)
        elif all_player_bounds[i][0][2]<hit_type<=all_player_bounds[i][0][3]:
          print("{} hits a single.".format(both_team_lineup_labels[team_up][i]))
          bases_advance(1)
        else:
          print("Error. The value given exceeds the range you need for this function. The hit_type was {}".format(hit_type))
          print("my ranges are ",all_player_bounds[i][0],)
      else:
        out_random=randint(0,6)
        if out_random==0:
          print("strike out for {}. That's out number {}".format(both_team_lineup_labels[team_up][i],outs+1))
        if out_random in range (1,3):
          print("{} hits a fly ball for out {}".format(both_team_lineup_labels[team_up][i], outs+1))
        if out_random==3:
          print("{}'s line drive caught by the shortstop for out {}.".format(both_team_lineup_labels[team_up][i],outs+1))
        if out_random==4:
          print("{} hits a blooper to first for out {}.".format(both_team_lineup_labels[team_up][i],outs+1))
        if out_random==5:
          print("{} gets out number {} on a grounder.".format(both_team_lineup_labels[team_up][i],outs+1))
        if out_random==6:
          print("{} hits and infield fly caught by the catcher for out {}.".format(both_team_lineup_labels[team_up][i],outs+1))
        outs=outs+1
      if i<=2 and outs==3:
       pitcher_message=randint(0,2)
       if pitcher_message==0:
        print("\n Great inning for the pitcher here.")
       if pitcher_message==1:
        print("\n The pitcher is really doing his job well.")
       if pitcher_message==2:
        print("\n Great performance from",team_names[team_up],"'s pitching.")
    if outs==3:
     inning=inning+1
     whose_on_first=[0,0,0]
     player_pos[0]=[0,0,0,0,0,0,0,0,0]
     player_pos[1]=[0,0,0,0,0,0,0,0,0]
     #print("the inning is ",inning)
     if inning==18 and runs[0]!=runs[1]:
       if runs[0]>runs[1]:
         print(team_names[0].upper(),"WIN!")
         print ("\nThe final score is\n", team_names[0],runs[0],"\n",team_names[1], runs[1])
         win=1
         print("==========================\nEND OF GAME\n==========================")
       if runs[1]>runs[0]:
         print(team_names[1].upper()," WIN THE GAME!")
         print ("\nThe final score is\n", team_names[0],runs[0],"\n",team_names[1], runs[1])
         win=1
         print("==========================\nEND OF GAME\n==========================")
     if win==0:
       print ("\nNow we head to the {}. The score is\n".format(inning_list[inning]), team_names[0],runs[0],"\n",team_names[1], runs[1])
       print("==========================\n",inning_list[inning],"\n==========================")
     #print (runs)
     if team_up==0:
      team_up=1
      inning_index[0]=i
     else:
        team_up=0
        inning_index[1]=i
        i=0

     #print("the TEAM UP is ",team_up)

    randindex=randindex+1
    #print("\n")
    if i<=7:
      i=i+1
    else:
      i=0
    if outs==3:
      i=0

while inning<18:
 run_inning()
 outs=0
while runs[0]==runs[1]:
 run_inning()



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent