This question is in python:
battleships = [['0','p','0','s'],
['0','p','0','s'],
['p','p','0','s'],
['0','0','0','0']]
def fun(a,b,bships):
c = len(bships)
return bships[c-b][a-1]
print(fun(1,1,battleships))
print(fun(1,2,battleships))
first print gives 0 second print gives p
I can't work out why, if you could give an explanation it would be much appreciated.
Thank you to those who help :)
Indexing starts at 0
. So battleships contains items at indexes 0
, 1
, 2
, 3
.
First len(bships)
gets the length of the list of lists battleships
, which is 4.
bships[c-b][a-1]
accesses items in a list through their index value. So with your first call to the function:
print(fun(1,1,battleships))
It's bships[4-1][1-1]
which is bships[3][0]
which is ['0','0','0','0'][0]
which is 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With