Hello, well i have written some code to try and simulate rolling a dice.. my program has to be able to play craps... here is how it should be played..
In a nutshell craps is played with two regular dice. You win a round of craps if your first roll is a 7 or 11 and you lose the round if your first roll is 2, 3 or 12 (called craps!). If any other number is rolled (4, 5, 6, 8, 9, 10) it must be rolled again before a 7 is rolled in order to win. If 7 is rolled first you lose.

The goal of the simulation is to simulate a games of craps where 4, 5, 6, 8, 9 and 10 are rolled on the first roll. We want to know the odds of eventually winning after each of these rolls.
The Design
Your Python program should get a value (4,5,6,8,9 or 10) from the user (bonus points if you check to make sure the input is correct and reprompt when wrong input is entered). The program will figure out the odds of winning if that value is rolled first. To do the computation the program will have to loop many times (10,000?) and simulate throwing dice. If a 7 is thrown, the player loses that round. If the point is thrown (4,5,6,8,9,10) then the player wins that round.
Your program must compute the percentage of rounds won for each of the points.

Hint on rolling dice
Python, of course, has no way of rolling real dice. To simulate the rolling of dice we will use the built-in random number function. We will choose a random number between 1 and 6 for the first die and another random number between 1 and 6 for the second die.
To generate a random number we can use the randrange function. But first, we have to bring this function into our Python program. This can be done with:
from random import randrange

To use the randrange function we just call it with two arguments, the lowest random integer that we want and one more than the highest integer that we want as in:
die_one = randrange(1,7)


Okay thats the desciption, Here is what i have written so far .. i am not sure about how to check for the rolls ..like if we roll a 7 or 11 , then we win but if we roll 2,3,or 12 then we lose. i'm not sure if i am even checking for the rolls.. .. i'd appreciate your help..

thanks

from random import randrange

def start():
try:
number = input("Enter 4, 5, 6, 8, 9, or 10: ")
except NameError:
print "Please enter a number"
start()
if number != 4 and number != 5 and number != 6 and number != 8 and number != 9 and number != 10:
print "Invalid number"
start()

if number == 7 or number == 11:
calculate(number)
print " You win Craps!!"

else:
calculate(number)

def calculate(number):
max = 1
x = 0
check = 0
lose = 0
again = 0
l = 0

while max < 1000:
print "Rolled ", x
die_one = randrange(1, 6)
die_two = randrange(1, 6)
x = die_one + die_two

if number == x:
check = check + 1

elif x == 7:
lose = lose + 1

else:
again = again + 1

max = max + 1

check = check/1000.00 * 100
again = again/1000.00 * 100
lose = lose/1000.00 * 100

print "Your chances of winning are ", check
print "Your chances of rolling again are ", again
print "Your chances of lossing are ", lose

start()

Recommended Answers

All 3 Replies

Hi,

please put your code in code tags, otherwise the indentation is lost :icon_wink:

if number != 4 and number != 5 and number != 6 and number != 8 and number != 9 and number != 10:

This can be written a bit shorter:

if number not in [4,5,6,7,8,9,10]:

I'll check the rest when it's properly formatted :icon_smile:

from random import randrange 

def start():
    try:
        number = input("Enter 4, 5, 6, 8, 9, or 10: ")
    except NameError:
        print "Please enter a number"
        start()
    if number not in [4,5,6,7,8,9,10]:
        print "Invalid number"
        start()

    if number == 7 or number == 11:
        calculate(number)
        print " You win Craps!!"

    else:
        calculate(number)

def calculate(number):
    max = 1
    x = 0
    check = 0
    lose = 0
    again = 0
    l = 0

    while max < 10:
        print "Rolled ", x
        die_one = randrange(1, 6)
        die_two = randrange(1, 6)
        x = die_one + die_two

        if number == x:
            check = check + 1

        elif x == 7:
            lose = lose + 1

        else:
            again = again + 1

        max = max + 1



    check = check/1000.00 * 100
    again = again/1000.00 * 100
    lose = lose/1000.00 * 100

    print "Your chances of winning are ", check
    print "Your chances of rolling again are ", again
    print "Your chances of lossing are ", lose

start()

Sorry, forgot to mention it: there should be not space after the "[" in code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.