0

Ther are 4 terms in a dictionary, each of which uses a random.randint() function. My issue is that I want to call a random term from the dictionary, then generate the random number and print the generated number and the term together. It is for a turn-based combat system, first generating a random move to use, then generating how much damage this will do. I hope the code below is helpful.

Enemy_Moves = {"Smackdown": random.randint(3,8),  # The name of the move, then the potential range of damage.
"Sleeper Hold": random.randint(1, 10),
"Charge": random.randint(1,3)}

Enemy_Move_Choice = random.choice(list(Enemy_Moves))   
Enemy_Damage = # The issue here
print("Enemy used ",Enemy_Move_Choice,". It deals ",Enemy_Damage," damage.")
Nimantha
  • 6,405
  • 6
  • 28
  • 69

3 Answers3

0

Solution

  • Change the value in each dictionary to a lambda function that returns the desired random value
  • Using Python variable naming convention

Code

enemy_moves = {"Smackdown": lambda:random.randint(3,8),  # The name of the move, then the potential range of damage.
              "Sleeper Hold": lambda:random.randint(1, 10),
              "Charge": lambda:random.randint(1,3)}

# Test Usage
for i in range(10):   # sequencing through 10 moves
    print(f'Move {i}')
    # Select random move and damage function
    enemy_move_choice, damage = random.choice(list(enemy_moves.items()))
    
    enemy_damage = damage()  # Evaluate damage

    print(f'Enemy used {enemy_move_choice}.  It deals {enemy_damage} damage')
    print()

Output

Note randomness of both move and damage.

Move 0
Enemy used Smackdown.  It deals 8 damage

Move 1
Enemy used Charge.  It deals 1 damage

Move 2
Enemy used Sleeper Hold.  It deals 3 damage

Move 3
Enemy used Smackdown.  It deals 3 damage

Move 4
Enemy used Sleeper Hold.  It deals 5 damage

Move 5
Enemy used Charge.  It deals 3 damage

Move 6
Enemy used Smackdown.  It deals 3 damage

Move 7
Enemy used Charge.  It deals 3 damage

Move 8
Enemy used Smackdown.  It deals 3 damage

Move 9
Enemy used Charge.  It deals 2 damage
DarrylG
  • 16,732
  • 2
  • 17
  • 23
  • This was exactly what I needed. I appreciate performing trials in your own answer as well. I'll be honest, I didn't know lambda functions existed. – Xavier Gillmer Feb 01 '21 at 23:29
  • @XavierGillmer--glad I could help. [Lambda aka Anonymous functions](https://en.wikipedia.org/wiki/Anonymous_function) are featured in most if not all modern languages to add elements of functional programming. So, it's something you will get to know. – DarrylG Feb 01 '21 at 23:56
-1

You can do it using a lambda function:


import random

dinamic_damage = lambda move: random.randint(move['min'], move['max'])
# using the enemy_moves dictionary, you give to the lambda the min and max numbers

enemy_moves = {
    "Smackdown": {'min': 3, 'max': 8}, 
    "Sleeper Hold": {'min': 1, 'max': 10},
    "Charge": {'min': 1, 'max': 3}
}

# chose a random move
enemy_move_choice = random.choice(list(enemy_moves))
# extract the dictionary value for that move, we can re-use the existent variable
enemy_move_choice  =  enemy_moves[enemy_move_choice]
# One value
enemy_damage = dinamic_damage(enemy_move_choice)
# Another random hit with the same move
another_enemy_damage = dinamic_damage(enemy_move_choice)
-2

You just need to access the value of the dictionary you created keeping everything else the same:

Enemy_Damage = Enemy_Moves[Enemy_Move_Choice]

You can also do it in one line:

Enemy_Move_Choice, Enemy_Damage = random.choice(list(Enemy_Moves.items()))
goalie1998
  • 1,427
  • 1
  • 9
  • 16
  • It would be a little better to explain the fact that this assigns two variables. It does solve the problem, yes, but being more verbose about it would help the explanation. Appreciate the help though. – Xavier Gillmer Feb 01 '21 at 23:53