-2

I am trying to make a very simple poker game in Python. Let's say I for example generate six random numbers by simply using the randint function. Would it be possible to cross-check all the numbers that get generated? An example of code could be this:

dice1=randint(1,6)
dice2=randint(1,6)
dice3=randint(1,6)
dice4=randint(1,6)
dice5=randint(1,6)
dice6=randint(1,6)

I know that I can do something along the lines of this to check if they are equal to eachother or not:

if dice1==dice2 && dice2==dice3 && dice3==dice4 && dice4==dice5 && dice5==dice6:
/////whatever function

The question is if it is possible to check if I can check if dice1==dice6 in a short and easy way, while also checking all the other dice their similarities or differences.

Beaver
  • 1
  • 5

3 Answers3

1

Use a list of dice instead of 6 variables, then you can do:

dice = [randint(1,6) for _ in range(6)]
print(all(die == dice[0] for die in dice))
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
  • Would it be possible to print the 6 random numbers? – Beaver Mar 09 '21 at 16:17
  • @Beaver `print(dice)`... – Tomerikoo Mar 09 '21 at 16:19
  • @Tomerikoo would it be possible to print something else than "False" – Beaver Mar 09 '21 at 16:21
  • @Beaver Your question(s) are really not clear. Please edit your question with a clear problem statement. Usually sample inputs and expected outputs help best to describe your problem. You can read about [ask] and how to provide a [mre] for more advice – Tomerikoo Mar 09 '21 at 16:24
0

If you only want to check if dice1 and dice6 are equal, you can just use the expression dice1 == dice6. If you want to check all of the elements, though, you can try this:

dice = [dice1, dice2, dice3, ... dice6]
if all([dice[0] == d for d in dice]):
    # Do something here

However, this method would not allow you to check which die does not equal the others.

Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
  • I want to be able to check all the variables and see if they match or not at once. If I wouldn't be able to check whether the die is equal to others this wouldn't be of any help right? – Beaver Mar 09 '21 at 16:16
  • @Beaver Can you clarify your question please? You haven't really said clearly what it is you're trying to do. Do you want to check if ***all*** dies are the same? – Tomerikoo Mar 09 '21 at 16:17
  • I indeed want to check all the die at the same time and be able to print all the generated numbers. So for example, you get a 5, 4, 2, 2, 6, 2, it would print those numbers, and tell how many numbers are equal – Beaver Mar 09 '21 at 16:19
  • @Beaver That's not, in any way, understandable from the question itself... – Tomerikoo Mar 09 '21 at 17:08
0

The quickest way is to create a set, because sets don't allow duplicates.

if len(set([dice1, dice2, dice3, dice4, dice5, dice6])) != 6:  # there are duplicates
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622