Write a program that rolls two dice until the user gets snake eyes. You should use a loop and a half to do this. Each round you should roll two dice (Hint: use the randint function!), and print out their values. If the values on both dice are 1, then it is called snake eyes, and you should break out of the loop. You should also use a variable to keep track of how many rolls it takes to get snake eyes.
Sample Run:
Rolled: 6 5
Rolled: 5 2
Rolled: 3 6
Rolled: 6 2
Rolled: 5 3
Rolled: 1 4
Rolled: 1 1
It took you 8 rolls to get snake eyes.
Here’s a simple Python program to roll two dice until you get snake eyes. It uses a loop to keep rolling until the condition is met and keeps track of the number of rolls.
import random
def roll_dice():
return random.randint(1, 6), random.randint(1, 6)
def main():
rolls = 0
while True:
die1, die2 = roll_dice()
rolls += 1
print(f'Rolled: {die1} {die2}')
if die1 == 1 and die2 == 1:
break
print(f'It took you {rolls} rolls to get snake eyes.')
if __name__ == "__main__":
main()
### Explanation:
1. Import the random module: This allows you to use `randint` to simulate rolling the dice.
2. Define a function `roll_dice()`: This function rolls two dice and returns their values.
3. In the `main()` function:
– Initialize a counter (`rolls`) to track the number of rolls.
– Use a `while True` loop to continuously roll the dice.
– After each roll, print the results.
– If both dice show a 1, break the loop.
4. Finally, print how many rolls it took to get snake eyes.
Feel free to ask if you have any more questions or
import random
# Initialize variables
rolls = 0 # To keep track of the number of rolls
while True:
# Simulate rolling two dice
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
rolls += 1
# Print the values of the dice
print(f”Rolled: {die1} {die2}”)
# Check if both dice show 1 (snake eyes)
if die1 == 1 and die2 == 1:
break # Exit the loop
# Print the result
print(f”It took you {rolls} rolls to get snake eyes.”)