0

So I'm testing out some mechanics for a text-based game I was going to make. If the player has armour it would halve the damage they take and if not they would take full damage. The problem I'm having is that whenever I try to run the functions twice, it resets the health because it has been hardcoded. So I'm wondering how can I get the function to overwrite the health variable after it runs each time?

Here is the code:

import random
inventory = ["Armour","Sword","Healing Potion"]
health=100

def randDmg():
    dealtDamage = random.randint(1,10)
    print("You have taken "+str(dealtDamage)+" damage.")
    return dealtDamage

def dmgCheck(damage, health):
    if "Armour" in inventory:
        damage = damage/2
    else:
        damage = damage
    health-=damage
    return health

print("Your new health is "+str(dmgCheck(randDmg(), health)))
  • 3
    Why not do `health = dmgCheck(randDmg(), health)`, and *then* use `print('...', health)` instead? – Martijn Pieters Jul 04 '15 at 15:02
  • possible duplicate of [Python passing an integer by reference](http://stackoverflow.com/questions/15148496/python-passing-an-integer-by-reference) – Quark Jul 04 '15 at 15:07
  • @MartijnPieters Thank you, I did not think of doing it like that. However, wouldn't that mean I would have to copy that snippet every time the player takes damage? – Dune raider Jul 04 '15 at 15:17
  • @Duneraider: you could use a `global health` statement in the `dmgCheck` function, but this is where classes and OO design come into their own right. – Martijn Pieters Jul 04 '15 at 15:20
  • @MartijnPieters Unfortunately I'm still learning Python and haven't covered classes yet. Thanks for the help though, I can just use your previous answer every time. – Dune raider Jul 04 '15 at 15:21

1 Answers1

0

Defining a global at the top of your dmgCheck function would work well - then you don't need to pass it in as a local. While you're at it, if you call the randDmg function within dmgCheck you won't need to pass that in either.

import random
inventory = ["Armour","Sword","Healing Potion"]
health=100

def randDmg():
    dealtDamage = random.randint(1,10)
    print("You have taken "+str(dealtDamage)+" damage.")
    return dealtDamage

def dmgCheck():
    global health
    damage = randDmg()
    if "Armour" in inventory:
        damage = damage/2
    else:
        damage = damage
    health-=damage
    return health

print("Your new health is" + str(dmgCheck()))
print("Your new health is" + str(dmgCheck()))

That last bit could also be simplified by using pythons' string formating syntax:

print("Your new health is %s" % dmgCheck())

To do something similar using a Python Class you could use:

import random

class Game(object):

    def __init__(self, inventory, health):
        self.inventory = inventory
        self.health = health

    def randDmg(self):
        dealtDamage = random.randint(1,10)
        print("You have taken "+str(dealtDamage)+" damage.")
        return dealtDamage

    def dmgCheck(self):
        damage = self.randDmg()
        if "Armour" in self.inventory:
            damage = damage/2
        else:
            damage = damage
        self.health -= damage
        return self.health

    def play(self):
        result = self.dmgCheck()
        print("Your new health is %s" % result)



game = Game(["Armour","Sword","Healing Potion"], 100)

game.play()
game.play()
plakias
  • 127
  • 1
  • 1
  • 5