Search code examples
pythonrandomintegergenerator

Random.randint input


I want the user to be able to input the least number possible and the greatest number possible and for the random.randint function to print a random number in between the numbers entered.

But the random.randint will only use integers in the parentheses. This is what I tried to use:

random.randint('Lnumber','Gnumber')
(input() == Lnumber (orGnumber))

of course, it comes up with an error.

how do I make python recognize the integer stored inside the variable(s) Lnumber and Gnumber and use them in random.randint?


Solution

  • An easier way is to take the input and use randrange to generate the number:

    mn, mx = map(int, input("Enter a min and max number separated by a space").split())
    print (random.randrange(mn,mx))
    

    Take the user input to get the min and max for the range, cast to ints using map:

    mn, mx = map(int, input("Enter a min and max number separated by a space").split())
    

    print (random.randrange(mn,mx)) will print a number in the range of min to max

    In your code: random.randint('Lnumber','Gnumber')

    you are passing strings to randint, not variables. You would need to take input as above or set the variables to a value then pass the variables cast as ints to randint:

    Lnumber = int(input("Enter a min"))
    Gnumber = int(input("Enter a max"))
    random.randint(Lnumber,Gnumber)