r/pythonhelp • • 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

2 comments sorted by

View all comments

3

u/carcigenicate May 13 '22

Don't make main recursive. Use an actual loop like a while instead. Every time you recurse (call main from main), you create a new goblin value.