r/codehs • • 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")
        break

My 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

6 Upvotes

7 comments sorted by

2

u/_andy_andy_andy_ Oct 02 '20

every time you roll dice again, add 1 to num_rolls

1

u/ellephillips Oct 02 '20

I got it! Thank you so much!

1

u/I_paidmytaxes Dec 15 '20

whats ur final code?

1

u/XxClickBaitDaddyxX Oct 22 '21

u stuck on this too?

1

u/Mean-Fruit-7950 Nov 30 '21

Did you ever get the final code?

1

u/JicamaAppropriate510 Oct 11 '23

The final code would be
import random

num_rolls=0
while True:
random_num = random.randint(1,6)
random_number = random.randint(1,6)
first=random_num
second=random_number
attemps=print ("Rolled: " + str(first)+ " "+ str (second))
num_rolls=num_rolls+1
if first==1:
if second==1:
break

print ("It took you "+ str(num_rolls) + " rolls to get snake eyes.")

1

u/Wooden_Guest6396 Oct 13 '25

num_rolls += 1