python - trying to get return info from one function to another -
i'm trying use functions (something inherently bad @ apparentlyxd) , trying return statement info first , use in second. both rolling dice, first function have first set returned, used in second function give user option reroll. doing wrong in implementation it's not recognizing original dieset list?
def rolldie(): die1 = 2 die2 = 2 die3 = 2 die4 = 4 die5 = 5 dieset = [die1,die2,die3,die4,die5] print(dieset) return dieset def reroll1(): rolldie() reroll1 = input("would reroll? yes or no: ") if reroll1 == "no": dieset = [die1,die2,die3,die4,die5] else: count = 0 times = int(input("how many die reroll? ")) while count < times: whichreroll = input("reroll die: ") if whichreroll == "1": die1 = random.randint(1,6) else: die1 if whichreroll == "2": die2 = random.randint(1,6) else: die2 if whichreroll == "3": die3 = random.randint(1,6) else: die3 if whichreroll == "4": die4 = random.randint(1,6) else: die4 if whichreroll == "5": die5 = random.randint(1,6) else: die5 dieset = [die1,die2,die3,die4,die5] count += 1 print(dieset) return dieset reroll1()
it's telling me "local variable 'die1' referenced before assignment" rolldie() comes first. if explain me appreciated:d
to able reference returned data rolldie() need assign variable i.e.:
dieset = rolldie()
however, have problem at:
if reroll1 == 'no': dieset = [die1, die2, die3, die4, die5]
here, referencing variables die1, die2... haven't been assigned value.
as workaround, assign these variables arbitrary value, 0:
dieset = [0, 0, 0, 0, 0]
but... in conditionals, reference variables die1, die2...; haven't been assigned. forget variable dieset, , assign die1, die2... directly:
die1, die2, die3, die4, die5 = rolldie() if reroll1 == 'yes' else [0, 0, 0, 0, 0]
this way, can reference values die1
etc. in following conditions.
hope helps
Comments
Post a Comment