-1

I have problem with this code

import random

class MyPlayer:

 def __init__(self, payoff_matrix, number_of_iterations=0):
    self.payoff_matrix = payoff_matrix
    self.number_of_iterations = number_of_iterations
    self.opponent_move = None
    self.opponent_move_history = list()


 def move(self): #move is random, True or False
    return random.choice(False, True)

 def record_opponents_move(self, opponent_move): #recording opponents moves
    self.opponent_move = opponent_move
    self.opponent_move_history.append(opponent_move)

Pycharm is still showing a TypeError: choice() takes exactly 2 arguments (3 given), I dont get it True and False are just 2 arguments? Or not?

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
chomaco
  • 51
  • 5

2 Answers2

4

Your random.choice() call should be like:

>>> import random
>>> random.choice([False, True]) # wrapped within list
True

Now coming to the issue. As per the random.choice() document, it receives only one parameter as sequence. But in the error you see takes exactly 2 arguments because choice() function is defined in Random class (source code). The class methods expects the first argument (also known as self) to function as the object of the same class. So here, random is passed as self.

Please also read What is the purpose of self in Python? for more clarity on this.

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • Thank you so much for helping me out. You are right, of course, that was the problem. – chomaco Oct 24 '16 at 19:54
  • @JozefSkála: No need of thanks. If the answer helped you, please mark [it as accepted](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – Moinuddin Quadri Dec 16 '16 at 22:17
0

random.choice takes an iterable, not separate arguments.

random.choice([True, False])
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895