Answer the question
In order to leave comments, you need to log in
Random in python, how to generate a number?
It is necessary not only to generate a number, it is necessary that such a number is not in the list
For example:
j = 10, 20, 30
randint(1, 40)
#нужно сделать так, чтобы числа из кортежа j никак не могли сгенерироваться
Answer the question
In order to leave comments, you need to log in
import random
def gen_num(min, max, bads):
while True:
rand = random.randint(min, max)
if rand not in bads:
return rand
j = 1, 2, 3, 4
print(gen_num(1, 40, j))
import random
j = [10, 20, 30]
def generateNum(j, minNum, maxNum):
generatedList = []
for x in range(minNum, maxNum):
if x not in j:
generatedList.append(x)
num = random.choice(generatedList)
return num
generateNum(j, 1, 40)
Greetings from kindergarten
1. generate,
2. check
3. if there is, then point 1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question