I have just started with Python and I am trying to make a function which generates 3 dices (using the random
module) and return True
if the 3 number can form 421 and return False
if not.
So I need to compare the result of the 3 dices and see if it can form the number 421 so there is more than one combination. I could just write a bunch of if statement whith every combination but if the number of combination was bigger I would need something way more effective.
I tried to generate all the winning combination and put them in a list call combi_poss
and compare it to a the cobination that I got call comb_d
however I can't compare this list of outcomes who by the way return that [(4, 2, 1), (4, 1, 2), (2, 4, 1), (2, 1, 4), (1, 4, 2), (1, 2, 4)]
to my current combination.
This is where I'm at right now
def alea_dice(s):
from random import randint,seed
from itertools import permutations
seed(s)
d1 = randint(1,6)
d2 = randint(1, 6)
d3 = randint(1, 6)
#print(d1,d2,d3)
solution=set([1,2,4])
comp_d=set([d1,d2,d3])
return comp_d == solution
print(alea_dice(24649))
So the above code is working.
But I don't understand what the function set()
is doing, and how it makes the code work.
So, if anyone has a better understanding of this, please share!
Aucun commentaire:
Enregistrer un commentaire