r/pythonhelp • u/4k23 • May 12 '22
The loop keep resetting a 'set' code.
from random import randint
print ("goblin(20 health)")
def attack(x):
damage = randint(1,6)
x = x - damage
print ("u did {} damage".format(damage))
if x > 0:
print ("no ded")
print("goblin has {} health left".format(x))
else:
print ("ez(u win)")
return x
def action(x):
user_answer = input("(type 'a' to attack):")
if user_answer == "a":
return attack(x)
else:
print ("bro just attack already")
def main():
goblin = 20
goblin = action(goblin)
for i in range(50):
main()
It keeps resetting the health everytime you attack. How to let the player keep attacking without setting health back to 20?
3
Upvotes
3
u/carcigenicate May 13 '22
Don't make
mainrecursive. Use an actual loop like awhileinstead. Every time you recurse (callmainfrommain), you create a newgoblinvalue.