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.
JavaScript
x
21
21
1
def find_armor(x):
2
if x=="bad armor":
3
armor=bad_armor
4
if x=="good armor":
5
armor=good_armor
6
7
##item
8
armor=[0,0]
9
bad_armor=["bad armor",300]
10
good_armor=["good armor", 500]
11
12
##choose
13
option1,option2=input(">").split(" ",1)
14
if option1=="equip":
15
find_armor(option2)
16
17
##ui
18
armor_name=armor[0]
19
armor_point=armor[1]
20
print("your armor:",armor_name,armor_point)
21
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:
JavaScript
1
9
1
def find_armor(x):
2
global armor
3
if x=="bad armor":
4
global bad_armor
5
armor=bad_armor
6
elif x=="good armor":
7
global good_armor
8
armor=good_armor
9