JLBarber
JLBarber

Reputation: 23

dice roll simulation in python

I am writing a program that is to repeatedly roll two dice until the user input target value is reached. I can not figure out how to make the dice roll happen repeatedly. Please, help...

from random import randrange
print ("This program rolls two 6-sided dice until their sum is a given target value.")
input = int (input("Enter the target sum to roll for:"))

#def main():

dice1 = randrange (1,7)

dice2 = randrange (1,7)

sumofRoll = dice1 + dice2

output = "Roll: {} and {}, sum is {}".format (dice1,dice2,sumofRoll)
print (output)

if sumofRoll == input:
    print ("All done!!")
if sumofRoll != input:


#how do I make this repeatedly run until input number is reached?!

Upvotes: 1

Views: 6270

Answers (5)

vash_the_stampede
vash_the_stampede

Reputation: 4606

I took your game and added some elements for you to peek through. One is you could use random.choice and select from a die list instead of generating new random ints repeatedly. Second you can use a try/except block to only accept an int and one that is within the range of (2, 13). Next we can add a roll_count to track the amount of rolls to hit our target. Our while loop will continue until target == roll_sum and then we can print our results.

from random import choice

die = [1, 2, 3, 4, 5, 6]

print('This program rolls two 6-sided dice until their sum is a given target.')
target = 0
while target not in range(2, 13):
    try:
        target = int(input('Enter the target sum to roll (2- 12): '))
    except ValueError:
        print('Please enter a target between 2 and 12')

roll_sum = 0
roll_count = 0

while target != roll_sum:

    die_1 = choice(die)
    die_2 = choice(die)
    roll_sum = die_1 + die_2
    roll_count += 1
    print('You rolled a total of {}'.format(roll_sum))

print('You hit your target {} in {} rolls'.format(target, roll_count))
...
You rolled a total of 4
You rolled a total of 12
You hit your target 12 in 29 rolls

Upvotes: 1

Sheldore
Sheldore

Reputation: 39042

Here is a complete working code which takes care of invalid sum entered as input as well. The sum of two dice rolls cannot be either less than 2 or more than 13. So a check for this condition makes your code slightly more robust. You need to initialize sumofRoll = 0 before entering the while loop as this would be needed to first enter the while loop. The value of 0 is a safe value because we excluded the value of 0 entered by user to be a valid sum.

from random import randrange 
print ("This program rolls two 6-sided dice until their sum is a given target value.") 
input_sum = int(input("Enter the target sum to roll for:"))

def main():
    sumofRoll = 0
    if input_sum < 2 or input_sum > 13:
        print ("Enter a valid sum of dices")
        return
    while sumofRoll != input_sum:
        dice1 = randrange (1,7)
        dice2 = randrange (1,7)
        sumofRoll = dice1 + dice2
        output = "Roll: {} and {}, sum is {}".format (dice1,dice2,sumofRoll) 
        print (output)
        if sumofRoll == input_sum: 
            print ("All done!!") 

main()  

This program rolls two 6-sided dice until their sum is a given target value.
Enter the target sum to roll for:10
Roll: 3 and 5, sum is 8
Roll: 6 and 6, sum is 12
Roll: 5 and 1, sum is 6
Roll: 2 and 5, sum is 7
Roll: 6 and 6, sum is 12
Roll: 3 and 5, sum is 8
Roll: 1 and 2, sum is 3
Roll: 6 and 4, sum is 10
All done!!

Upvotes: 1

user10444020
user10444020

Reputation:

Use a do while statement

do:
dice1=randrange(1,7)
...
while(sumofroll != input)
print("all done") 

Upvotes: 0

Vincent Rodomista
Vincent Rodomista

Reputation: 780

count = 0
while(sumOfRoll != input):
    dice1 = randrange(1,7)
    dice2 = rangrange(1,7)
    count = count + 1
    sumOfRoll = dice1 + dice2
print("sum = %s, took %s tries" % (sumOfRoll, count))

Upvotes: 0

Judismar Arpini Junior
Judismar Arpini Junior

Reputation: 431

This is a simple while loop:

sumofRoll = -1; #a starting value!
while sumofRoll != input:
  #Put the code that updates the sumofRoll variable here

Upvotes: 0

Related Questions