Machine Learning with Python Projects - Rock Paper Scissors

Tell us what’s happening:

Whenever I try to update the code lines for the errors in main.py, it returns errors in RPS_game.py. RPS_game.py is not supposed to be edited. It’s possible I may need to go back and review the lessons again and retry to project at a later time.

Final results: {'p1': 0, 'p2': 0, 'tie': 0}
Player 1 win rate: 0%
Traceback (most recent call last):
  File "/home/runner/boilerplate-rock-paper-scissors-1/main.py", line 7, in <module>
    play(player, abbey, 1000)
  File "/home/runner/boilerplate-rock-paper-scissors-1/RPS_game.py", line 13, in play
    p2_play = player2(p1_prev_play)
  File "/home/runner/boilerplate-rock-paper-scissors-1/RPS_game.py", line 92, in abbey
    last_two = "".join(opponent_history[-2:])
TypeError: sequence item 1: expected str instance, tuple found

Your code so far

import random

def player(prev_play, opponent_history=[], state_matrix=None):
    if state_matrix is None:
        state_matrix = {"R": {"R": 0, "P": 0, "S": 0},
                        "P": {"R": 0, "P": 0, "S": 0},
                        "S": {"R": 0, "P": 0, "S": 0}}

    if prev_play:
        opponent_history.append(prev_play)
        if len(opponent_history) > 1:
            last_play = opponent_history[-2]
            state_matrix[last_play][prev_play] += 1

    if len(opponent_history) < 2:
        guess = random.choice(["R", "P", "S"])
    else:
        last_opponent_play = opponent_history[-1]
        next_move = max(state_matrix[last_opponent_play], key=state_matrix[last_opponent_play].get)
        guess = {"R": "P", "P": "S", "S": "R"}[next_move]  # Play to beat the most likely next move

    return guess, opponent_history, state_matrix

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Machine Learning with Python Projects - Rock Paper Scissors

I have decided to go back and review, but I would still like to know why it was only creating an error for abbey.

Have you sorted out the problem? I think the problem lies in the return line of your player function. The player function is called by RPS_game.py, which is supposed to return a string representing the next move for it to play (“R”, “P”, or “S”) only, but your player function is returning a tuple of three values (guess, opponent_history, state_matrix), so it ends up to an error stating “expected str instance, tuple found”, and the error is raised during the execution of RPS_game.py. This error will be raised by any bot, not only for abbey, apparantly it looks so simply beacuse abbey is first called as the opponent.