Search code examples
pythonrandomdice

Error using `random.randint` "too many positional arguments"


I had problems when making a call to random.randint(). I got the following error message in my program. I am using python 3.8, and I am not sure why this is happening. Below is the error message.

Traceback (most recent call last):
  File "C:\Users\W\Desktop\All.py files\droll.py", line 13, in <module>
    roll=random.randint(1,
TypeError: randint() takes 3 positional arguments but 7 were given

Here is my code

import random
def rule():
    print ("Roll the die!")
    print ("If you get a 1, you lose,")
    print ("And if you get a 6, you win.")
    print ("Anything inbetween does not count.")

rule()
#Main game loop
while True:
    q = input ("Are you ready to roll? (Y/N)").lower().strip()
    if q == "y":
        roll=random.randint(1,
                            2,
                            3,
                            4,
                            5,
                            6)
        if (roll == 1):
            print ("You got a 1! You lost!")
        if (roll == 6):
            print ("You got a 6! You won!")
        else:
            print ("You got a middle roll!")
    if q == "n":
        print ("That's unfortunate.")

if anyone could help fix this problem, it would be much appreciated. I'm not sure if there is a new way to write random codes, and this has been bugging me for a while, as even the most simple code would not work. I've tried to fix this problem by adding a few more random.randints, and using if and statements, however that would sometimes result in 2 answers, or a blank space. If anyone has an answer, once again, I would much appreciate the help. Thanks


Solution

  • Yes, there are multiple ways to generate random (and pseudorandom) numbers, commonly used functions include:

    1. choice() :- used to generate a random number from a container.

      roll = random.choice([1, 2, 3, 4, 5, 6])

    2. randrange(beg, end, step) :- used to generate random number within a range specified (excluding end)

      roll = random.randrange(1, 7, 1)

    3. randint() :- Yet another way...

      roll = random.randint(1,6)