Ali R. - 1 year ago 97
Python Question

# Make dice values NOT repeat in if statement

I would like my dice values not to repeat because when it does, it registers a wrong input in my program (not a crash, simply a string message stating "Your input was wrong"). It is a board game so I do not want the same values to repeat, for example 6,0 to repeat twice or even thrice. Is there a way to save the dice values or anything I can do so that it chooses new random values every time?

``````dice = random.randint(0,3)
if dice == 0:
guess = str(random.randint(0,4))+','+str(random.randint(0,4))
elif dice == 1:
guess = str(random.randint(0,4))+','+str(random.randint(4,9))
elif dice == 2:
guess = str(random.randint(4,9))+','+str(random.randint(0,4))
else:
guess = str(random.randint(4,9))+','+str(random.randint(4,9))
``````

Desired output:

``````6,0
4,5
8,1
1,7
``````

with no repeats, such as:

``````6,0
8,2
6,0 #this is a repeat, I do not want this to happen
3,9
``````

Or you could just roll over and over again until a new combination emerges. This also implies that you have to do some bookkeeping of the combinations already drawn. And you must make sure that there is at least one more possible combination left, otherwise the loop will not terminate.

``````combis = []

dice = random.randint(0,3)

while True:
if dice == 0:
guess = str(random.randint(0,4))+','+str(random.randint(0,4))
elif dice == 1:
guess = str(random.randint(0,4))+','+str(random.randint(4,9))
elif dice == 2:
guess = str(random.randint(4,9))+','+str(random.randint(0,4))
else:
guess = str(random.randint(4,9))+','+str(random.randint(4,9))

if not guess in combis:
combis.append(guess)
break
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download