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
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
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.