Catt
Catt

Reputation: 3

Change an item in a list when iterating

I am making a game that requires the player to fight monsters. The user gets to tell the player where to move and a board is displayed. The board is just a list I have set to be spaces and then X when the player moves around on the list.

Example of what I mean:

['','','','X','']

Player moves left

['','','X','','']

When the player reaches a monster, the direction in which the player was heading is considered and that side of the board is filled with monsters so the player can't run past without fighting.

Example

['','','monster','X','']
#player moves left
#new list after the list is filled with monsters
['monster','monster','monster','','']

What I want to know is how would I go about filling that list when the user makes the player walk into a monster?

What I have now is as follows:

#note that num and my two lists are being predefined in this example. 
#Normally there is other code to determine where the monster
#goes and where the user is.

floor1 = ['','','X','','']
floor1_monsters = ['','','monster','','']
num = 2
user = X


if floor1[num] == user and floor1_monsters[num] == 'monster':
  if move == 'left':
    for i in (0,floor1[num]):
      floor[i] = 'monster'

The error I get when running this:

TypeError: list indices must be integers or slices, not str

num is the variable that was assigned to hold the position of the user at all times.

floor1 is the list that contains the user and the list that will be filled with monsters.

user is 'X'.

floor1_monsters is another list separate from floor1 that holds where a monster is.

The monster list and the list with the user are both 5 long, so num helps to alert the user when they have stepped into a space where a monster also occupies.

After the code is run I want floor1 to look like this:

['monster','monster','monster,'','']

I will provide more info if needed. Thanks in advance.

Upvotes: 0

Views: 30

Answers (1)

Mr. Kelsey
Mr. Kelsey

Reputation: 530

As stated by match in the comments, floor1[num] is going to grab the value in the list at index num. This will be either the value X or the value ''. His solution:

#note that num and my two lists are being predefined in this example. 
#Normally there is other code to determine where the monster
#goes and where the user is.

floor1 = ['','','X','','']
floor1_monsters = ['','','monster','','']
num = 2
user = "X"
move = "left"


if floor1[num] == user and floor1_monsters[num] == 'monster':
  if move == 'left':
    for i in range(num):
      floor1_monsters[i] = 'monster'

print(floor1_monsters)

Can be verified to work if the player is moving left. As this will fill the monster list from index 0 up to the index of X, monster overlap.

However, if your player were headed right, and you wanted a monster list of ['','','monster','monster','monster'], this route would fail.

In this case, you will need to run your iteration from the num index to the end of your list.

  if move == 'right':
    for i in range(num, len(floor1_monsters)):
      floor1_monsters[i] = 'monster'

Upvotes: 1

Related Questions