-3

I tried to make a simple combat system but it isn't working how I want it to. Eventually when I get near zero im stuck at number 4 usually. And what I want it to do is that if d_damage equals 0 to print out that the person won. Im mostly struggling on the damage for the opponent. I havent worked on the users health. But if someone could help I would dearly appreciate it.

Here is my code:

import random
import os

health = [ '100' ]
d_health = [ '100' ]

damage = random.randint(0, 7)

def clear():
    os.system("cls")

def health_damage():
    sum = int(d_health[0]) - damage
    if sum > 0:
        d_health.remove(d_health[0])
        d_health.append(str(sum))
        begin_game()
        
    if int(d_health[0]) <= 0:
        clear()
        print( "You defeated the dragon!" )

def begin_game():
    clear()
    print( "Dragon Health: " + str(d_health[0]) + "")
    print()
    print( "Your Health: " + str(health[0]) )
    x = input( "What do you wish to do?" )
    if x.lower() == "1":
        health_damage()
        begin_game()

def before_game():
    print( "Welcome to The Beginning: Fight to Survive" )
    x = input( "Are you sure you want to fight?" )
    if x.lower() == "yes":
        clear()
        begin_game()
before_game()
mashido_
  • 1
  • 2
  • 3
    Health can just be a number to simplify the code – Caridorc Feb 18 '23 at 17:24
  • 4
    Welcome to Stack Overflow! Please take the [tour]. This is a Q&A site, but you haven't asked a question, and it's not clear what exactly you need help with. Check out [ask]. See also [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/q/284236/4518341) – wjandrea Feb 18 '23 at 17:25
  • 2
    FWIW, for debugging tips, check out [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert and [How to step through Python code to help debug issues?](/q/4929251/4518341). Keep in mind your code is recursive. – wjandrea Feb 18 '23 at 17:51

1 Answers1

1

you only lower the health if health is above 0, so it's going to repeat forever for no reason

def health_damage():
sum = int(d_health[0]) - damage
if sum > 0:
    d_health.remove(d_health[0])
    d_health.append(str(sum))
    begin_game()
    
if int(d_health[0]) <= 0:
    clear()
    print( "You defeated the dragon!" )

just remove the sum part and directly remove the if statment. it doesnt help with anything.

you should instead do this:

d_health = 100
damage = random.int(1, 7)
def health_damage():

    d_health -= damage
    # the -= is a better looking version of d_health = d_health - damage
if int(d_health) <= 0:
    clear()
    print( "You defeated the dragon!" )
    while True:
        if input(""):
            quit()
else:
    clear()
    begin_game()

(btw a bit unrelated, but please learn object oriented programming. it sounds hard at first, but it makes everything way easier to work with)