Skip to content
Advertisement

Don’t understand why my function can’t be called

I try to make a little text based rpg with python. And the first thing that I want to make is an input to equip armor or weapons.

def find_armor(x):
    if x=="bad armor":
        armor=bad_armor
    if x=="good armor":
        armor=good_armor

##item
armor=[0,0]
bad_armor=["bad armor",300]
good_armor=["good armor", 500]

##choose
option1,option2=input(">").split(" ",1)
if option1=="equip":
    find_armor(option2)

##ui
armor_name=armor[0]
armor_point=armor[1]
print("your armor:",armor_name,armor_point)

So here is the problem:

my function find_armor is not calling whatever I enter.

If my first word is “equip” the output is always: your armor: 0 0

Advertisement

Answer

In a function, variables change with local scope. Then use global armor before. And fixing the bad indentation:

def find_armor(x):
  global armor
  if x=="bad armor":
    global bad_armor
    armor=bad_armor
  elif x=="good armor":
    global good_armor
    armor=good_armor
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement