#๐Ÿ”’ Help with combat system

12 messages ยท Page 1 of 1 (latest)

strange zephyr
#

I know I already posted earlier, but I forgot to mention some things.
What I am trying to fix is a problem with the health tracking. When I run this code and attack, it subtracts a number (2-8) from eHP (20). The standalone print statements afterprint ("You dealt " + damage + " damage") andpy x = damage(eHP) show the right number (12-18). However, the py print (enemy + " " + eHP + "/20 HP") line shows 20/20, reverting eHP to 20. I know the eHP = 20 reverts it, but I don't know how to have it turn off once the damage calculation changes eHP.
I don't know how to fix this and need help

import random
import time

def banditcombat(MCHP):
    enemy = "Bandit"
    eHP = 20
    print ("Suddenly, a bandit jumps from the bushes")
    print ("What will you do?")
    print (" ")
    check = 1
    turn(check, MCHP, eHP, MC, enemy)
    print ("hi")

def turn (check, MCHP, eHP, MC, enemy):
    turn2(check, MCHP, eHP, MC, enemy)

def turn2(check, MCHP, eHP, MC, enemy):
    MCHP = str(MCHP)
    eHP = str(eHP)
    print (MC + "    " + MCHP + "/50 HP")
    print (enemy + "    " + eHP + "/20 HP")
    print ("  Attack       Heal")
    turn3(check, MCHP, eHP, MC, enemy)
    eHP = int(eHP)
    if eHP <= 0:
        return
    else:
        turn2(check, MCHP, eHP, MC, enemy)
        return eHP

def turn3(check, MCHP, eHP, MC, enemy):
    combat1 = input()
    if combat1 == "attack" or combat1 == "Attack":
        x = damage(eHP)
        print (eHP)
        check = 2
    if combat1 == "heal" or combat1 == "Heal":
        heal()
        check = 2
    if check == 1:
        print("Invalid answer. Try again")
        turn2(check, MCHP, eHP, MC, enemy)
    return eHP

def damage(eHP):
     damage = random.randint (2, 8)
     damage = str(damage)
     print ("You dealt " + damage + " damage")
     eHP = int(eHP)
     damage = int(damage)
     eHP = eHP - damage
     damage = str(damage)
     print (eHP)
     return eHP

def heal():
     heal = random.randint (1, 4)
     heal = str(heal)
     print ("You recovered " + heal + " health")

MC = "Glorbo McFlorbo"
MCHP = 50
start = 1
banditcombat(MCHP)
print ("spots")
rigid ospreyBOT
#

@strange zephyr

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

strange zephyr
#

no

severe nacelle
severe nacelle
#

You have 2 problems:

  • all the variables inside functions are local variables and do not affect the outside (this is normal and good)
  • you're not making use of the return values

Example:

        x = damage(eHP)

You pass in eHP. Inside damage the local ePH is updated. And returns. And then you store than in x. The local ePH here where you call damage is not changed. You want:

eHP = damage(ePH)
#

@strange zephyr ^^

strange zephyr
#

I put that change in, but it still shows 20/20. Did I miss something?

severe nacelle
#

That change has to happen everywhere. Every time you call some function passing ePH as a parameter, and expect it to be changed afterwards, you need to get it bad as a return value.

rigid ospreyBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.