Jonah Isensee
Jonah Isensee

Reputation: 15

Pygame, how do I get a random output to repeat the same value twice?

I'm coding a game and I want to have damage that is random. Lets say I did this for the first half of the code:

damage = [1, 2, 3, 4, 5]
enemy_hp == enemy_hp - random.choice(damage)

When I run the code, it outputs a value 4, I want to get the same value a second time to print it on the screen for the player to see how much damage they dealt.

What would the second half of the code be? I've been stuck on this for a few hours now.

I don't know how confusing this is to read, so I'm going to give a 'quick' summary. I want to get the same value twice from a list using random.choice(). The second time I get that value it will be used in a line of code similar to this: print('random text here' + str(value))

Upvotes: 1

Views: 76

Answers (1)

Jiří Baum
Jiří Baum

Reputation: 6930

Put the result of random.choice() in a variable, then use it twice:

damage_dealt = random.choice(damage)
enemy_hp = enemy_hp - damage_dealt
print(f'Damage dealt: {damage_dealt}')

Note: if the game is for anything serious, you might want to use secrets.choice rather than random.choice.

Upvotes: 1

Related Questions