Why is this variable being assigned twice?

I’m trying to figure out where I went wrong by printing everything, so I noticed that the problem appears to start with “last_three”. Or rather, that’s where I can first tell that the function seems to be running the initial code twice instead of moving on to return the “ideal_response”. Can someone tell me what I’m doing wrong?

My issue likely stems from not fully understanding how the dictionaries’ keys and values are being called here. Namely, the [0][k] thing doesn’t make much sense to me. I understand what the dictionaries are meant to do within the function; what I don’t get that way of calling keys and value pairs. What’s the 0 doing there? Oddly, I can’t seem to find an explanation through google (that’s a first :sweat_smile:).

Thanks in advance!

def player(prev_opponent_play,
              "RRR": 0, "RRP": 0, "RRS": 0,
              "RPR": 0, "RPP": 0, "RPS": 0,
              "RSR": 0, "RSP": 0, "RSS": 0,
              "PRR": 0, "PRP": 0, "PRS": 0,
              "PPR": 0, "PPP": 0, "PPS": 0,
              "PSR": 0, "PSP": 0, "PSS": 0,
              "SRR": 0, "SRP": 0, "SRS": 0,
              "SPR": 0, "SPP": 0, "SPS": 0,
              "SSR": 0, "SSP": 0, "SSS": 0

  if not prev_opponent_play:

  last_two = "".join(opponent_history[-2:])
  last_three = "".join(opponent_history[-3:])
  play_order[0][last_three] += 1

  potential_plays = [
        last_two + "R",
        last_two + "P",
        last_two + "S",
  print("potential plays:",potential_plays)

  sub_order = {
        k: play_order[0][k]
        for k in potential_plays if k in play_order[0]

  prediction = max(sub_order, key=sub_order.get)[-1:]

  ideal_response = {'P': 'S', 'R': 'P', 'S': 'R'}
  return ideal_response[prediction]```

Quite frankly didn’t understand what you’re trying to do in the code
But I can definitely help you with the [0][k] thing.

So your play_order variable is actually a list and by doing player_order[0][k] you are accesing the first element of your list and then the key in the dictionary.

So, for eg:
if list is:
a_list = [{'a': 'b', 'b': 'c'}, {'c': 'd'}]
here the list contains two elements,
Now if I did a_list[0]['a']
then I would get 'b'

In this case the list has two elements but in your case the list has only one so everytime you are doing play_order[0][some_variable_k]

Hope this helps…

Oh crap you’re right, it’s a dictionary within a list. See this is an attempt at solving one of the Machine Learning with Python projects, Rock Paper Scissors, and to do so I am trying to sort of “improve” on one of the bots I have to beat by adding one more step to the Markov Chain.

But then when I run it - and since I added a bunch of print statements there as I said - “last_three” gets assigned twice, first “RRR” and then just “R” (which makes it even weirder). Since that value comes last it’s the one that counts, and since it doesn’t match what’s expected(three letters), the code falls apart there.

Your reply has helped me understand that list bit that I’d missed, so thanks! Sadly I still can’t figure out why the rest is happening.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.