Home > Blockchain >  How can I put the value of this three variables out of the function with the value inside the functi
How can I put the value of this three variables out of the function with the value inside the functi

Time:11-08

Where is the code, I want to take the WarriorTurnOrder, PriestTurnOrder and enemy_turnOrder, with the value inside the function.

def initiative_phase(): #Function for setting the turn order
    WarriorTurnOrder = 0
    PriestTurnOrder = 0
    enemy_turnOrder = 0
    
    WarriorTurnOrder = WarriorTurnOrder   random.randint(0,20)   warrior[4] #Random Value for the Warrior's turn number
    PriestTurnOrder = PriestTurnOrder   random.randint(0,20)   priest[4] #Random Value for the Priest's turn number
    enemy_turnOrder = enemy_turnOrder   random.randint(0,20)   vampire[4] #Random Value for the Vampire's turn number
    
    print("\nThe warrior got "   str(WarriorTurnOrder)) 
    print("The vampire got "   str(enemy_turnOrder))
    print("The priest got "   str(PriestTurnOrder))

I was trying to do a system of turn order but it keeps giving me an error or a 0 on each value.

def first_turn():
    if (WarriorTurnOrder > enemy_turnOrder) and (WarriorTurnOrder > PriestTurnOrder): #Warrior first
        warrior_turn()
    elif (PriestTurnOrder > enemy_turnOrder) and (PriestTurnOrder > WarriorTurnOrder): #Priest first
        priest_turn()
    elif (enemy_turnOrder > WarriorTurnOrder) and (enemy_turnOrder > PriestTurnOrder): #Enemy goes first
        enemy_turn()
    elif (WarriorTurnOrder == enemy_turnOrder) or (WarriorTurnOrder == PriestTurnOrder): #In case of draw return to turn order
        initiative_phase()
    elif (PriestTurnOrder == enemy_turnOrder) or (PriestTurnOrder == WarriorTurnOrder): #In case of draw return to turn order
        initiative_phase()
    elif (enemy_turnOrder == WarriorTurnOrder) or (enemy_turnOrder == PriestTurnOrder): #In case of draw return to turn
        initiative_phase()
        
def second_turn():
    if (WarriorTurnOrder > enemy_turnOrder) and (WarriorTurnOrder < PriestTurnOrder): #Warrior second
        warrior_turn()
    elif (PriestTurnOrder > enemy_turnOrder) and (PriestTurnOrder < WarriorTurnOrder): #Priest second
        priest_turn()
    elif (enemy_turnOrder > WarriorTurnOrder) and (enemy_turnOrder < PriestTurnOrder): #Enemy second
        enemy_turn()

def third_turn():
    if (WarriorTurnOrder < enemy_turnOrder) and (WarriorTurnOrder < PriestTurnOrder): #Warrior third
        warrior_turn()
    elif (PriestTurnOrder < enemy_turnOrder) and (PriestTurnOrder < WarriorTurnOrder): #Priest second
        priest_turn()
    else: #Enemy third
        enemy_turn()

CodePudding user response:

METHOD 1: All these variables that you declare in a function are local variables. Use global variable in your functions. For example: global WarriorTurnOrder. Do this for all 3 variables and it should work as you want.

def initiative_phase(): #Function for setting the turn order
    global WarriorTurnOrder
    global PriestTurnOrder
    global enemy_turnOrder

    WarriorTurnOrder = 0
    PriestTurnOrder = 0
    enemy_turnOrder = 0
    
    WarriorTurnOrder = WarriorTurnOrder   random.randint(0,20)   warrior[4] #Random Value for the Warrior's turn number
    PriestTurnOrder = PriestTurnOrder   random.randint(0,20)   priest[4] #Random Value for the Priest's turn number
    enemy_turnOrder = enemy_turnOrder   random.randint(0,20)   vampire[4] #Random Value for the Vampire's turn number
    
    print("\nThe warrior got "   str(WarriorTurnOrder)) 
    print("The vampire got "   str(enemy_turnOrder))
    print("The priest got "   str(PriestTurnOrder))

METHOD 2 (better practice): Use your variables as function arguments and add return statement with a tuple or a list of all 3 variables (this will return a tuple or a list with their values) at the end of your function. You can pass these values later in other functions.

def initiative_phase(): #Function for setting the turn order
    WarriorTurnOrder = 0
    PriestTurnOrder = 0
    enemy_turnOrder = 0

    WarriorTurnOrder = WarriorTurnOrder   random.randint(0,20)   warrior[4] #Random Value for the Warrior's turn number
    PriestTurnOrder = PriestTurnOrder   random.randint(0,20)   priest[4] #Random Value for the Priest's turn number
    enemy_turnOrder = enemy_turnOrder   random.randint(0,20)   vampire[4] #Random Value for the Vampire's turn number

    print("\nThe warrior got "   str(WarriorTurnOrder)) 
    print("The vampire got "   str(enemy_turnOrder))
    print("The priest got "   str(PriestTurnOrder))

    #the order of these variables is the order you need to unpack them
    return (WarriorTurnOrder, PriestTurnOrder, enemy_turnOrder)

Unpacking these values:

WarriorTurnOrder, PriestTurnOrder, enemy_turnOrder = initiative_phase()

Other functions should be define like this:

def function(WarriorTurnOrder, PriestTurnOrder, enemy_turnOrder):
    #put your code here
  • Related