Reputation: 23
So I am creating a python game in which you have to fight a monster. The player has 3 abilities which deal always the same damage. The monster does damage but it is random every time. I'd like to have the random damage number seperately so I am able to create a summary after every round for example if the Player did 100 dmg monster did 42 dmg your health would be displayed after this round. the player has 100 hp and it should update but not refresh every fight. so after round 1 it would say player hp left: 58hp monster hp left: 900hp round 2 player did 100 dmg momster 10 player hp left: 48hp monster hp left: 800hp
ability = input("Press A for a melee hit, B for a firespell and C for a thunder").capitalize()
if ability == "A":
print("Monster took 10 hp damage")
elif ability == "B":
print("Monster took 40 hp damage")
elif ability == "C":
print("Monster took 100 hp damage")
print("You have recieved that much damage from the monster: ", random.randrange(100))
I would be so happy if anyone can help me! Thanks in advance already!
Upvotes: 1
Views: 46
Reputation: 226231
To save a value for later use, store it in a variable:
ability = input("Press A for a melee hit, B for a firespell and C for a thunder").capitalize()
if ability == "A":
print("Monster took 10 hp damage")
elif ability == "B":
print("Monster took 40 hp damage")
elif ability == "C":
print("Monster took 100 hp damage")
damage = random.randrange(100)
print("You have recieved that much damage from the monster: ", damage)
health -= damage
In Python 3.8, assignment expressions let you do that right in the middle of a print():
ability = input("Press A for a melee hit, B for a firespell and C for a thunder").capitalize()
if ability == "A":
print("Monster took 10 hp damage")
elif ability == "B":
print("Monster took 40 hp damage")
elif ability == "C":
print("Monster took 100 hp damage")
print("You have recieved that much damage from the monster: ",
(damage := random.randrange(100)))
Upvotes: 1