This:
import random
def main():
userNumber1 = input("Choose a number from 1-20")
userNumber2 = input("Choose another number from 1-20")
if (userNumber1 == userNumber2):
print("Invalid input")
else:
num1 = random.Random(userNumber1,userNumber2)
num2 = random.Random(userNumber1,userNumber2)
num3 = random.Random(userNumber1,userNumber2)
while (num1==num2 or num1==num3 or num3==num2):
if (num3==num2):
del num2
num2 = random.Random(userNumber1,userNumber2)
elif (num1==num3):
del num3
num3 = random.Random(userNumber1,userNumber2)
elif (num1==num2):
del num2
num2 = random.Random(userNumber1,userNumber2)
else:
pass
print(str(num1) str(num2) str(num3))
main()
is giving back the error:
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
Does anyone know why and help me troubleshoot?
CodePudding user response:
random.Random
is a class. You construct an instance of it if you want your own random number generator based on a given seed or a given algorithm.
It looks like you're simply trying to construct some random integers in a range, so you're looking for randint
.
random.randint(a, b)
Return a random integer
N
such thata <= N <= b
. Alias forrandrange(a, b 1)
.
Note that the bounds are inclusive, so a
and b
are valid returns from this function. Adjust your bounds as needed if that's not intentional.
CodePudding user response:
If you're trying to generate random numbers between the user input bounds, the function you're looking for is random.randint
:
r = random.randint(3, 12) # -> returns a random integer 3 <= r < 12
CodePudding user response:
Use random.randint() instead of random.Random()
CodePudding user response:
First of all indent user Inputs 1 tab backwards,from the look of it you are trying to get a random no between user inputs for this purpose use Random.randint
here is the error free code .
import random
def main():
userNumber1 = int(input("Choose a number from 1-20"))
userNumber2 = int(input("Choose another number from 1-20"))
if userNumber1 == userNumber2:
print("Invalid input")
else:
num1 = random.randint(userNumber1,userNumber2)
num2 = random.randint(userNumber1,userNumber2)
num3 = random.randint(userNumber1,userNumber2)
while (num1==num2 or num1==num3 or num3==num2):
if (num3==num2):
del num2
num2 = random.randint(userNumber1,userNumber2)
elif (num1==num3):
del num3
num3 = random.randint(userNumber1,userNumber2)
elif (num1==num2):
del num2
num2 = random.randint(userNumber1,userNumber2)
else:
pass
print(str(num1) str(num2) str(num3))
main()