Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)
edited by

I try to make the little text-based rpg with the python. First thing I want to make is my 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)

This is my problem:

The function find_armor is not calling whatever I try to enter.

If my first word is the "equip" then my output is always: your armor: 0 0

1 Answer

0 votes
by (36.8k points)

In the function, variables change with local scope. Then use the global armor before. And fixing your 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

Improve your knowledge in data science from scratch using Data science online course 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...