I'm trying to append a string to the end of an array element by using the following function:
def spread(farm):
#writing potential fruit spread
for y in range(0,len(farm[0])):
for x in range(0,len(farm[0])):
#if this current space is a tree, write the potential spaces accordingly
if farm[y][x] == "F" or farm[y][x] == "W" or farm[y][x] == "G" or farm[y][x] == "J" or farm[y][x] == "M":
for b in [-1,0,1]:
#making sure the y-coord is within the bounds of the farm
if y+b >= 0 and y+b < len(farm[0]):
for a in [-1,0,1]:
#making sure the x-coord is within the bounds of the farm and the selected space is not a tree
if x+a >= 0 and x+a < len(farm[0]) and farm[y+b][x+a] != "F" and farm[y+b][x+a] != "W" and farm[y+b][x+a] != "G" and farm[y+b][x+a] != "J" and farm[y+b][x+a] != "M":
#if space is blank, write over the space outright
if farm[y+b][x+a] == "_":
farm[y+b][x+a] = farm[y][x].lower()
else:
#wherein my troubles lie :(
farm[y+b][x+a] = farm[y+b][x+a] + farm[y][x].lower()
return farm
with the following input, an array (in farm):
[['_' '_' '_' 'F' '_' '_' '_']
['_' '_' '_' 'W' '_' '_' '_']
['_' '_' '_' '_' '_' '_' '_']
['_' '_' '_' 'J' '_' '_' '_']
['_' '_' '_' '_' '_' '_' '_']
['_' 'G' '_' '_' '_' 'F' '_']
['W' '_' '_' '_' '_' '_' 'G']]
What the function is supposed to do is to simulate spreading fruit trees. Every tree (represented by a capital letter) will spread to the adjacent squares (represented by a lowercase character or underscore). However, the very last line handles the case in which the selected array element is not an underscore. What is supposed to happen is that it will append the string to the end of the array element instead of replacing it, but instead appends nothing. The output is supposed to look something like this:
[['_' '_' 'fw' 'F' 'fw' '_' '_']
['_' '_' 'fw' 'W' 'fw' '_' '_']
['_' '_' 'wj' 'wj' 'wj' '_' '_']
['_' '_' 'j' 'J' 'j' '_' '_']
['g' 'g' 'jg' 'j' 'jf' 'f' 'f']
['gw' 'G' 'g' '_' 'f' 'F' 'fg']
['W' 'gw' 'g' '_' 'f' 'fg' 'G']]
But instead it outputs this:
[['_' '_' 'f' 'F' 'f' '_' '_']
['_' '_' 'f' 'W' 'f' '_' '_']
['_' '_' 'w' 'w' 'w' '_' '_']
['_' '_' 'j' 'J' 'j' '_' '_']
['g' 'g' 'j' 'j' 'j' 'f' 'f']
['g' 'G' 'g' '_' 'f' 'F' 'f']
['W' 'g' 'g' '_' 'f' 'f' 'G']]
What am I doing wrong?