Machine Learning with Python Projects - Rock Paper Scissors

Tell us what’s happening:
Describe your issue in detail here.

My code runs fine in Pycharm but when I switch to Replit I get this error:

Traceback (most recent call last):
File “/home/runner/boilerplate-rock-paper-scissors/main.py”, line 6, in
play(player, quincy, 1000)
File “/home/runner/boilerplate-rock-paper-scissors/RPS_game.py”, line 12, in play
p1_play = player1(p2_prev_play)
File “/home/runner/boilerplate-rock-paper-scissors/RPS.py”, line 180, in player
update_matrix(pair_diff2[-1])
File “/home/runner/boilerplate-rock-paper-scissors/RPS.py”, line 152, in update_matrix
matrix[pair][prev_play][‘n_obs’] = matrix[pair][prev_play][‘n_obs’] + 1
KeyError: ‘’
exit status 1

Your code so far

import random

def player(
prev_play,
opponent_history=,
my_history=[‘S’],
matrix={
‘PP’: {
‘R’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘P’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘S’: {
‘prob’: 0.33333,
‘n_obs’: 0
}
},
‘PR’: {
‘R’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘P’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘S’: {
‘prob’: 0.333333,
‘n_obs’: 0
}
},
‘PS’: {
‘R’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘P’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘S’: {
‘prob’: 0.333333,
‘n_obs’: 0
}
},
‘RP’: {
‘R’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘P’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘S’: {
‘prob’: 0.33333,
‘n_obs’: 0
}
},
‘RR’: {
‘R’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘P’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘S’: {
‘prob’: 0.333333,
‘n_obs’: 0
}
},
‘RS’: {
‘R’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘P’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘S’: {
‘prob’: 0.333333,
‘n_obs’: 0
}
},
‘SP’: {
‘R’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘P’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘S’: {
‘prob’: 0.33333,
‘n_obs’: 0
}
},
‘SR’: {
‘R’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘P’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘S’: {
‘prob’: 0.333333,
‘n_obs’: 0
}
},
‘SS’: {
‘R’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘P’: {
‘prob’: 0.333333,
‘n_obs’: 0
},
‘S’: {
‘prob’: 0.333333,
‘n_obs’: 0
}
}
},
pair_diff2=,
pair_diff1=[‘SS’]
):
decay = 0.7
keys = [‘R’, ‘P’, ‘S’]
beat = {‘R’: ‘P’, ‘P’: ‘S’, ‘S’: ‘R’}

def rand_predict():
    return random.choice(['R', 'P', 'S'])

def update_matrix(pair):
    for i in keys:
        matrix[pair][i]['n_obs'] = decay * matrix[pair][i]['n_obs']

    matrix[pair][prev_play]['n_obs'] = matrix[pair][prev_play]['n_obs'] + 1

    n_total = 0
    for i in keys:
        n_total += matrix[pair][i]['n_obs']

    for i in keys:
        matrix[pair][i]['prob'] = matrix[pair][i]['n_obs'] / n_total

def predict(pair):
    comp = []

    for i in keys:
        comp.append(matrix[pair][i]['prob'])

    if max(comp) == min(comp):
        return rand_predict()
    else:
        for i in keys:
            if matrix[pair][i]['prob'] == max(comp):
                return i

pair_diff2.append(pair_diff1[-1])
pair_diff1.append(my_history[-1] + prev_play)
opponent_history.append(prev_play)

if len(pair_diff2) != 0:
    update_matrix(pair_diff2[-1])
    output = beat[predict(pair_diff1[-1])]
else:
    output = rand_predict()

my_history.append(output)

return output

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 OPR/95.0.0.0

Challenge: Machine Learning with Python Projects - Rock Paper Scissors

Could you share the link to your Replit for review? Kinda hard to review on here especially as all your code isn’t in the code quotes above.

Seems to maybe not like the ' ' symbols in the key… wondering if maybe during the copy and paste, perhaps its a ` symbol instead of a ’ symbol.

So… well, the code in this Replit is completely different than the code you pasted into this post previously. Did you change it?

With this replit, the error I get when I try to run it is in this line:

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

Stating that it was an empty string(meaning sub_order) was empty.

I wanted to try a different approach. I changed it back to the original. Sorry.

Like the error stated you are getting a keyError, stating that key ’ ’ is not valid in the following line:

matrix[pair][prev_play]['n_obs'] = matrix[pair][prev_play]['n_obs'] + 1

I believe you are getting this because when an opponent first starts, the prev_play value is blank. Therefore in this line, you are attempting to use a blank key, which isn’t valid:

EX: matrix[pair][' ']['n_obs']

It worked. Thank you. Thank you. Thank youuuuu.

1 Like

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