-3

I have Python code that is trying to get the position of a player. However, I always get an error ('cave_' is not defined).

My (error causing)code looks like:

player_pos = cave_(player_pos)[n] #n is a number

The cave and player_pos variables have been defined and player_pos is a tuple. I expect player_pos to be updated so that its value is something like '5'.

The entire snippet of code looks like:

    global player_pos
    cave_0 = (1,4,0,0)
    cave_1 = (2,11,2,0)

and so on 11 times. Then the offending block of code.

How can this be debugged?

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
John Doe
  • 3
  • 2
  • What are you trying to achieve by doing this? – Sayse Jan 08 '18 at 08:34
  • What error are you getting? what is `cave_`? please read [ask] and [mcve] – DeepSpace Jan 08 '18 at 08:35
  • 2
    @DeepSpace - The op is trying to access a variable dynamically (i.e `cave_1`). Most likely, an xy problem – Sayse Jan 08 '18 at 08:38
  • 3
    Well, the first step in debugging is reading the error and traceback, but you haven't provided either of those. – roganjosh Jan 08 '18 at 08:41
  • You shouldn't be using variables like this. Make `cave` a dictionary of tuples or tuple of tuples, so you can do something like `cave[player_pos][n]` – Bryan Oakley Jan 08 '18 at 17:42
  • Appears to be duplicate of https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables – Foon Jan 08 '18 at 18:17

1 Answers1

0

Your syntax is wrong for what you are trying to do. Consider this statement:

player_pos = cave_(player_pos)[n]

First, this will call a function cave_() with the argument player_pos. Next, this will try to extract the nth item from the result. Finally, that result will be assigned to player_pos.

If you have not defined a variable named cave_, this statement will fail.

It sounds like you are trying to define a sequence of caves and then access an item in that sequence. One way to do that is as follows:

caves = [
    (1,4,0,0),
    (2,11,2,0),
    ...
]

player_pos = caves[player_pos[n]]
augurar
  • 12,081
  • 6
  • 50
  • 65