I’m a fairly new programmer, but I am trying to make a fighting game code, but I have been stumped on one part, within my code I am trying to make sure that when the enemy health = 0, it will end. I can make the program end, well. But I don’t want to enemy health to go under 0, it can work with me fine, but I really want to change this.
import random gameactive = 0 x= 100 while gameactive == 0: if x > 0: crit = random.randint(8,9) base = random.randint(5,6) a = random.randint(1,10) if a == 10: x -= crit print("nITS A CRITICAL HIT!") # Dont mind this please, this chunk about critical hits. print("n") print(str(x) + " Health left") else: x -= base print("n") print(str(x) + " Health left") else: break
so what happens when the program is ran, it will generate numbers and decreases x using those generated numbers. But when running the program, I want the code to only limit itself 0 health and not go under. Sorry if i horribly explained it simply put, I want a way so x will be capped at 0, and it will not print negative numbers and instead print some sample text like enemy died.
Advertisement
Answer
use max function like t his:
import random gameactive = 0 x= 100 while gameactive == 0: if x > 0: crit = random.randint(8,9) base = random.randint(5,6) a = random.randint(1,10) if a == 10: x -= crit print("nITS A CRITICAL HIT!") # Dont mind this please, this chunk about critical hits. else: x -= base print("n") print(str(max(x,0)) + " Health left") else: break