Python negative index?

75 views Asked by At

I have simple question about prisoners dilemma. It is mainly just to reassure that I am doing this correctly.

In the code below [-1] means that it will see if your last move is collude and if their last move is betray. And you could go even further back in the histories using [-2] or [-3]

elif my_history[-1]=='c' and their_history[-1]=='b':
    return 'b'
else:
    return 'c'
1

There are 1 answers

2
Jonathan March On BEST ANSWER

Since this question has nothing to do with Prisoner's Dilemma, I've retitled it to reflect what appears to be your actual question. I've also removed the canopy tag since this question seems to be strictly about pure Python (a very active topic here), nothing specifically about Canopy.

If your question is "does history[-1] yield the last element of the history list", then the answer is Yes. You can verify this at the Python prompt with something like this:

history = list('abcde') print history print history[-1]