简体   繁体   中英

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 ?

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)

What I would do is store an input to a variable and then take that variable and pass it into the random.randint() command.

import random

firstNumber = int(input('Enter Your Min number')
secondNumber = int(input('enter Your Max number')

You can then either store the random number with a variable

thirdNumber = random.randint(firstNumber,secondNumber)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM