The most common answer for the 4.11.4 Snake Eyes CodeHS is:
import random
# Enter your code here
num_rolls = 0
while True:
num_rolls += 1
dice_one = random.randint(1, 6)
dice_two = random.randint(1, 6)
print("Rolled: " + str(dice_one) + " " + str(dice_two))
if (dice_one == 1 and dice_two == 1):
print("It took you " + str(num_rolls) + " rolls to get snake eyes.")
break
It correctly implements a loop to roll two dice until both dice show a 1, which is referred to as “snake eyes” in dice games. Here’s your code with proper indentation for clarity:
import random
# Initialize variables
roll_count = 0
while True:
# Roll two dice
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
roll_count += 1
# Print the values rolled
print(f"Rolled: {die1} {die2}")
# Check for snake eyes (both dice are 1)
if die1 == 1 and die2 == 1:
break
# Output the total number of rolls
print(f"It took you {roll_count} rolls to get snake eyes.")
This code will continuously roll two six-sided dice (using random.randint(1, 6)
) until both dice come up as 1.
The number of rolls it takes to achieve this outcome is tracked by the num_rolls
variable, which is incremented with each roll.
When snake eyes are rolled, the program prints how many rolls it took and then exits the loop with break
.
Just run this code in a Python environment to see how it works. The number of rolls it takes to get snake eyes can vary greatly due to the randomness.
Leave a comment