Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add exception to random.randint in Python?

Tags:

python

random

So I have a variable that creates a random number between 0 and 10, however, I would not like the random number created to be 5. How do I go about adding exceptions to random.randint in Python? What I have below is not doing this:

number = random.randint(0, 10) !=5

This is only returning True or False based on whether the random number equals 5 or not... how do I fix this?

like image 730
CoopDaddio Avatar asked Dec 07 '22 19:12

CoopDaddio


1 Answers

You can do

number = random.randint(0,10)
while number == 5:
   number = random.randint(0,10)
like image 84
JGut Avatar answered Jan 09 '23 04:01

JGut