944,179 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2927
  • Python RSS
Apr 23rd, 2007
0

Need Help on my Program

Expand Post »
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()
Last edited by jtxay; Apr 23rd, 2007 at 2:13 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jtxay is offline Offline
11 posts
since Dec 2004
Apr 23rd, 2007
0

Re: Need Help on my Program

Hi,

please put your code in [ code=Python]...[/code], otherwise the indentation is lost
Python Syntax (Toggle Plain Text)
  1. 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:
Python Syntax (Toggle Plain Text)
  1. if number not in [4,5,6,7,8,9,10]:
I'll check the rest when it's properly formatted
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Apr 25th, 2007
0

Re: Need Help on my Program

Click to Expand / Collapse  Quote originally posted by mawe ...
Hi,

please put your code in [ code=Python]...[/code], otherwise the indentation is lost
Python Syntax (Toggle Plain Text)
  1. 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:
Python Syntax (Toggle Plain Text)
  1. if number not in [4,5,6,7,8,9,10]:
I'll check the rest when it's properly formatted
[ code=Python]
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()

[/code]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jtxay is offline Offline
11 posts
since Dec 2004
Apr 25th, 2007
0

Re: Need Help on my Program

Sorry, forgot to mention it: there should be not space after the "[" in [code]
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Help with wxpython program
Next Thread in Python Forum Timeline: image viewer tool for Blender





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC