Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I put the value of this three variables out of the function with the value inside the function?

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading