# ML with Python - Issue with understanding the Markov Chain code

Hey everyone. As the title says, I’m having some trouble. I (think) I understand the way Abbey’s Markov chain works, and I’m trying to add one more step to it. Problem is that apparently, I at the very least don’t understand dictionaries well enough because I keep hitting the same problem and I don’t know why.

As you’ll see in my code below, I used some print statements to try to see where I went wrong, and sure enough “last_two” and “potential_plays” are returning two sets of values each instead of one. I can’t figure out why.

Sidenote: I’m aware that even after solving this issue I’ll need to tweak the code to be able to beat all 4 bots, but this is where I’m stuck right now.

Thanks in advance for any help!

``````def player(prev_opponent_play,
opponent_history=[],
play_order=[{
"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:
opponent_history=["R","R"]
prev_opponent_play = 'R'
opponent_history.append(prev_opponent_play)

last_two = "".join(opponent_history[-2:])
print("last two:", last_two)

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

potential_plays = [
last_two + "R",
last_two + "P",
last_two + "S",
]
print(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]

``````

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