23.0k views
3 votes
Yahtzee is a dice game that uses five die. There are multiple scoring abilities with the highest being a Yahtzee where all five die are the same. You will simulate rolling five die 777 times while looking for a yahtzee.

Program Specifications :

Create a list that holds the values of your five die.

Populate the list with five random numbers between 1 & 6, the values on a die.

Create a function to see if all five values in the list are the same and IF they are, print the phrase "You rolled ##### and its a Yahtzee!" (note: ##### will be replaced with the values in the list)

Create a loop that completes the process 777 times, simulating you rolling the 5 die 777 times, checking for Yahtzee, and printing the statement above when a Yahtzee is rolled.

1 Answer

3 votes
Sure, here's the Python code to simulate rolling the five dice and checking for a Yahtzee:

```python
import random

# Function to check if it's a Yahtzee
def is_yahtzee(dice):
return all(die == dice[0] for die in dice)

# Simulating rolling the dice 777 times
for i in range(777):
# Populating the list with five random numbers between 1 & 6
dice = [random.randint(1,6) for _ in range(5)]

# Checking for a Yahtzee and printing the statement if true
if is_yahtzee(dice):
print(f"You rolled {dice} and it's a Yahtzee!")
```

In this code, we first define a function `is_yahtzee` that takes a list of dice values and checks if all the values are the same.

Then, we use a loop to simulate rolling the dice 777 times. For each roll, we create a list `dice` with five random numbers between 1 and 6 using a list comprehension.

Finally, we check if it's a Yahtzee by calling the `is_yahtzee` function with the `dice` list as an argument. If it is a Yahtzee, we print the statement with the dice values.
User Lucask
by
8.8k points