I'm trying to extract a certain amount of numbers from a range of 17 numbers. However if a number is not valid (decided by the user) I need to extract another unique nuber that is different from the number not valid and the numbers that has been extracted. This is my code:
import random
import rich
from rich.console import Console
console = Console()
num = int(console.input('[yellow]How many number you want to extract[/yellow]: '))
non = int(console.input('[yellow]Number you want to exclude[/yellow]: '))
x = random.sample(range(1,18),num)
console.print("[yellow]Numbers extracted: [/yellow]",str(x))
for i in x:
if i == 1:
if non != 1:
console.print('[green]:heavy_check_mark:[/green] Baiesi')
else:
console.print('❌ Baiesi')
riserva = random.sample(range(1,18),1)
notvalid = True
while notvalid:
if i not in riserva or x[i] not in riserva:
sosti = random.sample(range(1,18),1)
print(sosti)
notvalid = False
else:
notvalid = True
print('unique nuber not valid')
#copy the code written before for every number until 17
The problem with this code is that the unique number that is supposed to be extracted is not unique: sometimes it gives numbers already extracted. Does someone have any idea on how to do this?
Your solutions is too complicated. Just create the list of numbers, remove the forbidden number from it and extract the sample: