Home > Software design >  Having troubles appending inside a range loop
Having troubles appending inside a range loop

Time:12-19

import random

def In_Play():
    return random.randint(0,100)

def pitch():
    return random.randint(0,100)

first_pitch_result = []
second_pitch_result = []
third_pitch_result = []
forth_pitch_result = []
fifth_pitch_result = []
sixth_pitch_result = []
seventh_pitch_result = []

pa_result = []

for i in range(0, 1000):
    if pitch()<=43:
       Fpitch = "Count 1-0"
    elif pitch()>=59:
       Fpitch = "Count 0-1"
    else:
       Fpitch = "0-0 In-Play"
       if 18<= In_Play()<41:
           pa_end = "0-0 Single"
       elif 10<= In_Play()<18:
           pa_end = "0-0 Double"
       elif 0<= In_Play()<1:
           pa_end = "0-0 Triple"
       elif 1<= In_Play()<10:
           pa_end = "0-0 Home Run"
       else:
           pa_end = "0-0 OUT"
           
           pa_result.append(pa_end)
               
    first_pitch_result.append(Fpitch)

I am having troubles being able to create the pa_result bucket? When one of the plate appearances end i want it to be dumped into the pa_result (later there will be at the plate results) but for the first pitch i want to end the pa (or i) and then put it into the pa_result but its just keeping the last one. While i am here, i was wondering if its better to continue on under each count, or figure out what plate apperances are left and continue with a new range and if statements. Subtracting the 0-0 In Play with 1-0 and 0-1 Counts.

CodePudding user response:

for i in range(0, 1000):
if pitch()<=43:
   Fpitch = "Count 1-0"
elif pitch()>=59:
   Fpitch = "Count 0-1"
else:
    Fpitch = "0-0 In-Play"
    if 18<= In_Play()<41:
        pa_end = "0-0 Single"
    elif 10<= In_Play()<18:
        pa_end = "0-0 Double"
    elif 0<= In_Play()<1:
        pa_end = "0-0 Triple"
    elif 1<= In_Play()<10:
        pa_end = "0-0 Home Run"
    else:
        pa_end = "0-0 OUT"
    pa_result.append(pa_end)
first_pitch_result.append(Fpitch)

Not sure if this was your issue but pa_result.append(Fpitch) was indented such that only when pa_end = "0-0 OUT" did it get appended to pa_result. If you move the indentation back one it appends to the result anytime the pitch is in play

  • Related