Need Help on my Program

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2004
Posts: 11
Reputation: jtxay is an unknown quantity at this point 
Solved Threads: 0
jtxay jtxay is offline Offline
Newbie Poster

Need Help on my Program

 
0
  #1
Apr 23rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Need Help on my Program

 
0
  #2
Apr 23rd, 2007
Hi,

please put your code in [ code=Python]...[/code], otherwise the indentation is lost
  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:
  1. if number not in [4,5,6,7,8,9,10]:
I'll check the rest when it's properly formatted
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 11
Reputation: jtxay is an unknown quantity at this point 
Solved Threads: 0
jtxay jtxay is offline Offline
Newbie Poster

Re: Need Help on my Program

 
0
  #3
Apr 25th, 2007
Originally Posted by mawe View Post
Hi,

please put your code in [ code=Python]...[/code], otherwise the indentation is lost
  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:
  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]
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Need Help on my Program

 
0
  #4
Apr 25th, 2007
Sorry, forgot to mention it: there should be not space after the "[" in [code]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2017 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC