0

I made a simple Yahtzee game in as few of lines as I could think. Currently, the user must press Enter (with any value) to continue. I would like to use a loop statement so that the dice continue to roll until Yahtzee (all rolled numbers are the same). I would also like a 10 second timer. What is the best way to add a loop statement to this code? P.S. This is not homework, I wanted to make this game for my Yahtzee nights. My daughter wakes easil...haha

import random

while True:
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    dice3 = random.randint(1,6)
    dice4 = random.randint(1,6)
    dice5 = random.randint(1,6)
    numbers = (dice1, dice2, dice3, dice4, dice5)
    sum1 = sum(numbers)
    if  sum1 == ("5" or "10" or "15" or "20" or "25"):
        print("Winner, winner, chicken dinner!", dice1, dice2, dice3, dice4, dice5)
    else:
        print("Your rolls are: ", dice1, dice2, dice3, dice4, dice5)
    input("Press return key to roll again.")

EDIT: This is my final product. Thank you for all the help guys!!

import random
import time
input("Press return key to roll.")
for x in range(0,10000):
    numbers = [random.randint(1,6) for _ in range(5)]
    if all(x == numbers[0] for x in numbers):
        print("Winner, winner, chicken dinner!", numbers)
        input("Press return key to play again.")

    else:
        print("Your rolls are: ", numbers)        
        print("Next roll in one second.")
        time.sleep(1)   
  • `sum1 == ("5" or ...)` condition is true only when `sum1` is `'5'`(string). What you need is `sum1 in (5, 10, ...)`. `sum1` is integer and you need to test with integer (`5` not same as `'5'`). – Austin Dec 07 '18 at 15:21
  • You have a loop (`while True:`). Add a line where you compare if all numbers are the same. If this is true `break` out of the loop. Replace `input` with a call to [`time.sleep`](https://docs.python.org/3/library/time.html#time.sleep). – Matthias Dec 07 '18 at 15:21
  • And of course it would be easier to drop those numbered variables `dice1`, `dice2` and so on. Just create a list: `numbers = [random.randint(1, 6) for _ in range(5)]`. – Matthias Dec 07 '18 at 15:29
  • @Matthias Thank you! That shortend my code by a lot ha ha – Jared Hammel Dec 07 '18 at 15:39

2 Answers2

0

Replace the while True:... with a while boolean_variable: ... and set the value of that boolean_variable to True before the while loop and to False when you achieved Yahtzee => in the if condition.

What do you mean by 10 second timer, however? A sleep time of ten seconds can be implemented by a time.sleep(10) at the end of the inner while loop.

EDIT boolean_variable EXAMPLE

import time
...
not_yahtzee = True
while not_yathzee:
    ....
    if sum == 5 or sum == 10 or ... :
        ...
        not_yahtzee = False
    else:
        ...
        time.sleep(10)

The ... represent the code you already have. As commented on your question, the if condition should look more like this one. There are other ways on how to check all the elements in a list are the same.

SBylemans
  • 1,764
  • 13
  • 28
0

If you would like to check if all the dice numbers are the same it is as simple as.

allDice = [dice1, dice2, dice3, dice4, dice5] #List of dice variables
if all(x == allDice[0] for x in allDice): # If all dice are the same
    print("Yahtzee")
    break # Break out of while loop

The most simple way of having a "timer" could be adding a time.sleep(). You have to import time otherwise it won't work.

So replace input("Press return key to roll again.") with time.sleep(10)

This means every 10 seconds the dice will roll until all dice are the same number, if they are then it prints Yahtzee and stops the loop.

Stubbs
  • 193
  • 1
  • 7