Machine Learning with Python Projects - Rock Paper Scissors

Tell us what’s happening:

import random

def player(prev_play, opponent_history=[]):
    """
    The player function returns the next move based on the previous move of the opponent.
    
    :param prev_play: The opponent's previous move ("R", "P", or "S")
    :param opponent_history: The history of the opponent's moves so far.
    
    :return: The player's next move ("R", "P", or "S")
    """
    
    # First move, choose randomly (since there's no history yet)
    if prev_play == '':
        return random.choice(['R'

Your code so far

Your browser information:

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

Challenge Information:

Machine Learning with Python Projects - Rock Paper Scissors

1 Like

There are a few fixes you need to make. Also, please format your code properly, as it currently appears incomplete.

  1. fix opponent_history=[] and so the default value is set to the empty list.
  2. handle the first move so if prev_play='' then program picks a random move.
  3. now also track the opponent history so for every round, opponent moved is stored in opponent_history.
  4. now the code plays the move that should counter the opponent’s move.
  5. Also, in your code, the random.choice() function has an unfinished argument. Please format your code properly so it’s easier to review.

Let us know if you still need any help. Good luck!

your code is incomplete

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').