-2

Sorry for not giving enough info on the problem. I have 3 files in replit, one being RPS_game, that has the code that simulates the games of RPS, those being a function called play that compare the option selected and determines who won the round and returns number of wins and winrate and then some players defined that use different algorithms. Another file is the RPS file which is the file where I have to program the algorithm to beat the rest of the players. Finally I have the main file that calls the function play from RPS_game giving as a parameters both players, one from RPS_game and the other being the player i have to program from the RPS file. To solve the problem i need to identify what player i am against, looking at the function of play in RPS_play i saw that player2 variable was passed as a parameter but in the code it doesnt assign a value to it. This is every line of code where the variable player2 is used in the function play

    def play(player1, player2, num_games, verbose=False):
    p1_prev_play = ""
    p2_prev_play = ""
    results = {"p1": 0, "p2": 0, "tie": 0}

    for _ in range(num_games):
        p1_play = player1(p2_prev_play)
        p2_play = player2(p1_prev_play)

I've tried to import the function to my piece of code and assign to a local variable the value of the funtcion:

    from RPS_game import play
    player2=play.player2
    if player2 =='quincy' or player2 =='kris':

but it gives me this error: AttributeError: 'function' object has no attribute 'player2' So i dont know how to get the info of what opponent i have right now to use an if statement. Hope this clarifies my problem.

  • Hard to tell without more context, but you probably want to make `play` a class with different methods (among others what's now your `play` function) and attributes. – tobias_k Aug 25 '23 at 09:10
  • Have you checked the code for RPS_game? – DarkKnight Aug 25 '23 at 09:29
  • Does this answer your question? [Access a function variable outside the function without using "global"](https://stackoverflow.com/questions/19326004/access-a-function-variable-outside-the-function-without-using-global) – Peter Aug 25 '23 at 09:43
  • 1
    The exercise is [here](https://github.com/a-mt/fcc-rock-paper-scissors/tree/master). You really, *really* need to read the instructions very carefully first. In particular, pay attention to this: "Do not modify `RPS_game.py`. Write all your code in `RPS.py`. For development, you can use `main.py` to test your code". The point of the exercise is to work out the best stategy *purely based on the history of previous plays* (which is recorded in the `opponent_history` argument). If you want to analyse the stategy of the other players, play them against each other using `main.py`. – ekhumoro Aug 25 '23 at 09:52

0 Answers0