thisguy hasaname
thisguy hasaname

Reputation: 1

Changing a local variable in multiple functions in Python?

background: I'm currently writing a text-based adventure and each enemy has a certain amount of turns you can attack it before it attacks back. So to handle this the code sets an argument in the function for the fight dictating how many times you can attack.

def fight_sequence(rounds):
  while rounds > 0:
    attack = input()
    if attack == magic:
      magic("you teleport away to safety. Congratulations you have stayed alive through your journey and found a few souvenoirs. nice job!", 1, "you muster up all of your energy, chant the spell.... and nothing happens.Cthulu... seems unimpressed", 1, "")
    elif attack == sword:
      sword(1)
def magic(teleportmessage, teleportsuccess, firemessage, firefail, winmessage):
  x = 0
  while x == 0:
    fightorflight = input("""you open the book to cast a spell
    Do you want to try teleporting or use a fireball?""").lower()
    if "teleport" in fightorflight:
      if teleportsuccess = 1:
        print(teleportmessage)
        x = 1
      else:
        choice = input("You can't teleport out of this battle. Would you like to try a fireball?")
        if choice == yes:
          fightorflight = "fireball"
        else:
          x = 1
    elif "fire" in fightorflight:
      print(firemessage)
      if firefail == 1:
        choice = input("do you want to try to teleport instead?").lower()
        if "yes" in choice:
          fightorflight = "teleport"
        else:
          x = 1
      else:
        print(winmessage)
    else:
      print("Sorry not sure what you mean")
def sword(attacksuccess):
  if attacksuccess == 1:
    print("You pull out the sword and swing at the monster damaging it severely.")
  else:
    print("You pull out the sword and swing at the monster, but its immune to blunt objects.")
fight_sequence(3)

both magic() and sword() need to be able to decrease rounds by 1, originally i just did that before entering the magic or sword function. however some items to attack with allow you to attack more than once if you want so that won't work for them. Such as magic if they also choose to teleport. Is there a way to allow me to change the variable rounds while inside of another function? I think using a return value might help but I'm not sure how to go about it

Upvotes: 0

Views: 1841

Answers (2)

user11115921
user11115921

Reputation:

I would recommend using classes to create this game rather than lots of functions, an example of a class in a hero game below.

class Hero:
   def __init__(self):
       self.health = 10
   def eatApple(self):
       self.health += 1
   def takeDamage(self):
       self.health -= 1

init function runs as class is initialized.

player = Hero()
print(player.health) # will print 10
player.takeDamage() 
print(player.health) # will print 9 

This way you can have global variables for you functions which can be changed in each function and is much more organised.

Upvotes: 0

tadge
tadge

Reputation: 96

You can simply add a new argument to the magic function, and pass the 'rounds' variable through when you call it.

e.g.

def fight_sequence(rounds):    
...
    magic("some message", false, "fired", false, "you won", rounds)

def magic(teleportmessage, teleportsuccess, firemessage, firefail, winmessage, rounds):

Your passing through the variable (not just the value) so it will change in every context where rounds can be seen.

HTH.

Upvotes: 1

Related Questions