Mirza Taimour
Mirza Taimour

Reputation: 1

choice() takes 2 positional arguments but 3 were given

import random
class Environment(object):
    def __init__(self):
        self.locationcondition={'A': '1' , 'B': '1' }
        
self. locationcondition['A']=random.choice(0,1)
self. locationcondition['B']=random.choice(0,1)

Upvotes: 0

Views: 17363

Answers (2)

Abdelrahman Emam
Abdelrahman Emam

Reputation: 393

Hello Mirza glad you joined our community,

Short Answer: Pass [0, 1] as a list

self. locationcondition['A']=random.choice([0,1])
self. locationcondition['B']=random.choice([0,1])

Long Answer:

in Python random.choice takes a list or set, ..etc. as an argument for example

my_list = [1, 2, 3, 4, 5]
a = random.choice(my_list)
print(a) # it will print random value from my_list

Although keep in mind that Python relies heavily on indentation so after fixing the choice error you will encounter another error that both lines

self.locationcondition['A']=random.choice(0,1)
self.locationcondition['B']=random.choice(0,1)

are not in the correct indentation, and they should be like this (Corrected Code)

import random
class Environment(object):
    def __init__(self):
        self.locationcondition={'A': '1' , 'B': '1' }
        
        self.locationcondition['A']=random.choice([0,1])
        self.locationcondition['B']=random.choice([0,1])

Upvotes: 1

Stephen
Stephen

Reputation: 1518

You need to give random.choice a sequence of options.

A sequence can be something like a tuple, a list, or even a generator.

In your case you should use something like: random.choice((0,1))

Upvotes: 0

Related Questions