Search code examples
pythonfunctionreturn

Return and Assignment Issue


I am still relatively new to coding. I am currently having a issue returning a value from a function. The basic idea, is that the player and an enemy roll 4 dice. I have set up an array that sorts the player's and the enemy's dice roll from highest value to lowest value. From here, I wanted to have the player's highest value dice to be subtracted from the enemy's highest value dice, for each roll pair (i.e. Player's First Roll - Enemy Player's First Roll or Enemy's First roll - Player's First Roll). I wanted to use the this formula to determine the damage to be done to the player or enemy. The code I have come up with is:

def dmg_calculator_by_dice_roll(player_highest_roll_one, enemy_highest_roll_one):
        if player_highest_roll_one > enemy_highest_roll_one:
            roll_one_dmg_to_enemy = (enemy_highest_roll_one - player_highest_roll_one) * -1
            roll_one_dmg_to_player = 0
            return roll_one_dmg_to_player
            return roll_one_dmg_to_enemy
        elif player_highest_roll_one < enemy_highest_roll_one:
            roll_one_dmg_to_player = (player_highest_roll_one - enemy_highest_roll_one) * -1
            roll_one_dmg_to_enemy = 0
            return roll_one_dmg_to_player
            return roll_one_dmg_to_enemy 
        else:
            roll_one_dmg_to_player = 0
            roll_one_dmg_to_enemy = 0
            return roll_one_dmg_to_player
            return roll_one_dmg_to_enemy

player_rolls_round_one = [8, 6, 2, 1]
enemy_rolls_round_one = [6, 2, 2, 1]
player_highest_roll_one = player_rolls_round_one[0]
enemy_highest_roll_one =  enemy_rolls_round_one[0] 
roll_one_dmg_to_player = dmg_calculator_by_dice_roll(player_highest_roll_one, enemy_highest_roll_one)
roll_one_dmg_to_enemy = dmg_calculator_by_dice_roll(player_highest_roll_one, enemy_highest_roll_one)   
dmg_calculator_by_dice_roll(player_highest_roll_one, enemy_highest_roll_one)


print(roll_one_dmg_to_enemy)
print(roll_one_dmg_to_player)

The issue I am running into is that the value of both roll_one_dmg_to_player and roll_one_dmg_to_enemy are continuously matching in value. If I were to run this example, the result I would expect the code to print is "2" and "0". But instead it is returning "2" and "2". What am I doing wrong? Thank you for any help.


Solution

  • You can't have double return statements, the second one is never reached

    Change this

    return roll_one_dmg_to_player
    return roll_one_dmg_to_enemy
    

    To this return (roll_one_dmg_to_player, roll_one_dmg_to_enemy)

    Then your function call should look like this

    roll_one_dmg_to_player, roll_one_dmg_to_enemy = dmg_calculator_by_dice_roll(player_highest_roll_one, enemy_highest_roll_one)
    

    That will give you a tuple