0

I am having trouble being able to print reference something from a list by using 2 variables.

currentRow=4
currentRowText="row"+str(currentRow)
currentCol=3
currentLocType=currentRowText[currentCol]
print(currentRow,">",currentRowText,">",currentCol,">",currentLocType)

This prints out

4 > row4 > 3 > 4

however the last variable should be 'city' as it should be getting the data from

row4=("grasslands", "forest", "forest", "city", "forest", "forest", "mountain")

Any help is appreciated.

Thanks in advance.

Ryan Cain
  • 43
  • 1
  • 7
  • What is the content of `currentRowText` – SparkAndShine May 09 '16 at 17:38
  • 1
    `currentRowText[currentCol]` gets the (3+1)st character of the string "row4", not the element at location 3 in the set named `row4`. How to get that depends on how you're storing your rows. –  May 09 '16 at 17:38
  • Thanks. The way that i am storing the rows is like above where i have row 4, there are rows row1-7, it is for a simple map based text game so this should be the last part i need to make it work (hopefully) – Ryan Cain May 09 '16 at 17:49

1 Answers1

0

Looks like you should replace this:

currentLocType=currentRowText[currentCol]

With:

currentLocType=row4[currentCol]

currentRowText currently contains a string, not a reference to a variable. Maybe you mean to do something like:

currentLocType=eval(currentRowText)[currentCol]

Which means interpreting currentRowText's string as code. Using eval will turn 'row4'[currentCol] into row4[currentCol]. If this is what you intend to do, first read about the disadvantages of using eval.

Community
  • 1
  • 1
kmaork
  • 5,722
  • 2
  • 23
  • 40