Python Forum
random.choice() takes two positional arguments, but three were given.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random.choice() takes two positional arguments, but three were given.
#1
Hello.

In Python, I am using the random module. I have used the random.choice() method to randomly pick from two strings:

    import random

    variable = random.choice('string1','string2')
    print(variable)
However, when I run it, the following error occurs:

Error:
TypeError: choice() takes two positional arguments, but three were given.
I have also used

from numpy import random

instead of just

import random

but it still does not work.

How am I able to fix this error? Thank you.
Reply
#2
Should use a list
import random as rnd
mylist = []

for i in range(10):
   mylist.append(i)
print(rnd.choice(mylist))
print(rnd.choice(mylist))
print(rnd.choice(mylist))
Output:
9 2 8
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply
#3
(May-31-2020, 12:36 PM)menator01 Wrote: Should use a list
import random as rnd
mylist = []

for i in range(10):
   mylist.append(i)
print(rnd.choice(mylist))
print(rnd.choice(mylist))
print(rnd.choice(mylist))
Output:
9 2 8

I think you have misunderstood my question or have replied to mine accidentally?
Reply
#4
Random may be a class therefore uses self as first argument.
Someone please correct me if I'm wrong.

That may be why your getting that error.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply
#5
(May-31-2020, 12:16 PM)ShakeyPakey Wrote: Hello.
In Python, I am using the random module. I have used the random.choice() method to randomly pick from two strings:
    import random
    variable = random.choice('string1','string2')
    print(variable)

The random.choice() method is expecting a sequence (list, tuple, or string) as its argument. The syntax you are using makes it seem like you are passing two separate strings as two separate arguments, when you mean for them to be seen as a single sequence. You can make this work properly by enclosing your two strings inside another set of parentheses so that it is recognized as a single tuple when random.choice() is called:
variable = random.choice(('string1','string2'))
The other, probably more common option would be to assign your strings to a tuple or list first, then call random.choice() with that variable:
tuple_choices = ('string1', 'string2')
variable = random.choice(tuple_choices)
Reply
#6
If you type "help(random.choice) you get this:
Help on method choice in module random:

choice(seq) method of random.Random instance
    Choose a random element from a non-empty sequence.
The two interesting pieces of information are that the function is expecting a "sequence" argument, and that the function is not a function.

In Python, a "sequence" is a container data type. Strings are sequences because they are made up of individual letters that you can access. Lists and tuples because they too are made up of things that can be independently accessed.

Your call "random.choice('string1','string2')" would have worked if you left out the second string. It would return a randomly chosen letter from 'string1'. Probably not what you want. To randomly choose between 'string1' and 'string2' you need to put them in a sequence. Because they are smaller and faster I would use a tuple.
random.choice(('string1','string2'))
But why did your error message say "choice() takes two positional arguments, but three were given." Where does it see three positional arguments? This was the other interesting thing learned from help. random.choice is not a function.

If you type "type(random.choice)" it returns <class 'method'>. choice is a method of class Random. It doesn't look like a method. Normally a method is called using an instance; instance.method(), but when you do this:
import random
x = random.choice(('string1', 'string2'))
the "random" is a module, not an instance of class Random. random tells Python to look in the "random" namespace to find "choice" So how can random.choice() call a method and where is the instance?

This confusing syntactic magic is performed using a singleton. When you import random it creates a single instance of class Random. "_inst = Random()". Then it creates some global variables to access the Random methods "choices = _inst.choices". This makes "Random" look like a collection of functions instead of a class, but you are really creating an instance and calling instance methods. When you call "random.choice('string1', 'string2')" your are actually calling "random._inst.choice(string1', 'string2')" which is really "random.Random.choice(_inst, string1', 'string2')". Look at that! Three positional arguments.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: max_value() missing 2 required positional arguments: 'alpha' and 'beta' Anldra12 2 358 May-15-2021, 04:15 PM
Last Post: Anldra12
  Unable to use random.choice(list) in async method spacedog 4 286 Apr-29-2021, 04:08 PM
Last Post: spacedog
  Class Takes No Arguments horuscope42 4 1,570 Oct-26-2020, 11:10 PM
Last Post: not_username1234
  Help with a random.randint choice in Python booponion 5 543 Oct-23-2020, 05:13 PM
Last Post: deanhystad
  trying to input a variable using random.choice python63 9 874 Aug-13-2020, 05:37 PM
Last Post: python63
  Random Choice Operations Souls99 6 731 Jul-31-2020, 10:37 PM
Last Post: Souls99
  How to reuse positional arguments entered in terminal. rcmanu95 1 530 Jul-04-2020, 01:00 AM
Last Post: bowlofred
  Class takes no arguments Nazartfya 2 1,023 Jun-27-2020, 12:45 PM
Last Post: Nazartfya
  Why Car() takes no arguments louis216 2 767 Jun-25-2020, 03:16 AM
Last Post: louis216
  TypeError: add() missing 2 required positional arguments NectDz 5 1,522 May-28-2020, 02:54 PM
Last Post: BitPythoner

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020