Game Development Reference
In-Depth Information
You can see that the 10 value has been removed from the x list. The remove() method
removes the first occurrence of the value you pass it, and only the first. For example, type
the following into the shell:
>>> x = [42, 5, 10, 42, 15, 42]
>>> x.remove(42)
>>> x
[5, 10, 42, 15, 42]
Notice that only the first 42 value was removed, but the second and third ones are still
there. The remove() method will cause an error if you try to remove a value that is not in
the list:
>>> x = [5, 42]
>>> x.remove(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>>
After removing the found treasure chest from the chests list, we return the string
'You have found a sunken treasure chest!' to tell the caller that the guess
was correct. Remember that any changes made to the list in a function will exist outside the
function as well.
85. else:
86. if smallestDistance < 10:
87. board[x][y] = str(smallestDistance)
88. return 'Treasure detected at a distance of %s
from the sonar device.' % (smallestDistance)
89. else:
90. board[x][y] = 'O'
91. return 'Sonar did not detect anything. All
treasure chests out of range.'
The else block executes if smallestDistance was not 0 , which means the player
did not guess an exact location of a treasure chest. We return two different strings,
depending on if the sonar device was placed within range of any of the treasure chests. If it
was, we mark the board with the string version of smallestDistance . If not, we mark
the board with a '0' .
Search WWH ::




Custom Search