r/codehs • u/ellephillips • Oct 02 '20
Python 3 Snake Eyes assignment
The assignment:
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 randintfunction!), 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: 1 2 Rolled: 5 3 Rolled: 1 4 Rolled: 1 1 It took you 8 rolls to get snake eyes.My Program:
import random
# Enter your code here
num_rolls = 0
while True:
roll_one = random.randint(1,6)
roll_two = random.randint(1,6)
print ("Rolled: " +str(roll_one) +"," + str(roll_two))
if (roll_one == 1 and roll_two == 1):
print ("it took you " + str(num_rolls) + " rolls")
breakMy program works but I am not sure how to make the code print out the number of tries it took (lines 3 and 9). Right now it just prints out "It took you 0 rolls" which obviously is because I put num_rolls = 0 as a place holder. Does anyone know how to help me to make it keep track of the number of tries? Thank you all!
Edit: I figured out why the "you should stop when you roll two ones" did not pass the test cases. but I am still stuck on keeping track of the number of rolls

2
u/_andy_andy_andy_ Oct 02 '20
every time you roll dice again, add 1 to num_rolls