Help with Python Craps Simulator

This is the place for queries that don't fit in any of the other categories.

Help with Python Craps Simulator

Postby digiwx9 » Wed Apr 16, 2014 11:48 am

Hi All,

I've been having some trouble getting the "point" portion of the code to run successfully. If someone could give me a hand I would appreciate it. I thought that if I put the code into a function it would run when that function was called. However I don't think that's the case.

I've benign fighting with this for hours and haven't gotten any further.

Thanks in advance.

Code: Select all
from Dice import PairOfDice
global Val
#
def pointCheck():
    gamestatus = '' 
    while gamestatus == "CONTINUE": #Checking the point
            if myPoint == Val:
             gamestatus == "WON"
            elif myPoint == 7:
             gamestatus = "LOST"
            if gamestatus == "WON":
             print("Winner! You Beat me")
            else:
             print("Sorry, Seven out!")
            print ("Roll Again?")
#
def MainDouble():
    bdice = PairOfDice()
    doubleDiceRoll = ''
    global myPoint #declare global variable
    raw_input("Press Enter to Roll the Dice once")

    while doubleDiceRoll == '': #Roll the dice (both die objects)
        bdice.toss()
        print ('The first die reads.. ' + str(bdice.getValue1()) + '\n')
        print ('The second die reads.. ' + str(bdice.getValue2()) + '\n')
      
        Val = bdice.getTotal()
               
        if Val == 7 or Val == 11:
          gamestatus = "WON"
                         
      
        elif Val == 2 or Val == 3 or Val == 12:
          gamestatus = "LOST"
             
        if Val != 7 and bdice.getTotal() != 11 and Val != 2 and Val != 3 and Val != 12:
          gamestatus = "CONTINUE"
            #
          myPoint = Val
          print"The Point is now " , myPoint #display the user's point value
     doubleDiceRoll = raw_input("Roll Again?")
        pointCheck()
       

           
       
MainDouble()

[/code]
Last edited by metulburr on Wed Apr 16, 2014 12:26 pm, edited 1 time in total.
Reason: init post, added code tags
digiwx9
 
Posts: 5
Joined: Wed Apr 16, 2014 11:43 am

Re: Help with Python Craps Simulator

Postby metulburr » Wed Apr 16, 2014 1:29 pm

what is the traceback error you get, and if you dont... what is the expected outcome that you didnt get?
classes would suit this well as you wont have to throw around a point or gamestate variable from funciton to function, and wont have to use globals

Ill post some fixes though:
Code: Select all
if Val != 7 and bdice.getTotal() != 11 and Val != 2 and Val != 3 and Val != 12:

this entire line could just be:
Code: Select all
else:

in a if/elif/elif structure, because if it is not [7,11] or [2,3,12], then it must be
Code: Select all
if Val != 7 and bdice.getTotal() != 11 and Val != 2 and Val != 3 and Val != 12:

or just else

you can simplify this line:
Code: Select all
elif Val == 2 or Val == 3 or Val == 12:

to
Code: Select all
if Val in [2,3,12]:


keep code indentation at the same level, the preferred is 4 spaces. Your code is all over the place.
Code: Select all
    while gamestatus == "CONTINUE": #Checking the point
            if myPoint == Val:
             gamestatus == "WON"

Code: Select all
          print"The Point is now " , myPoint #display the user's point value
     doubleDiceRoll = raw_input("Roll Again?")
        pointCheck()


in python, it is normal to only capitalize a class name and that is it
Code: Select all
def MainDouble():
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1499
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help with Python Craps Simulator

Postby digiwx9 » Wed Apr 16, 2014 2:02 pm

Hi Metulburr,

Thanks for your advice, I'll look into those changes.

As for the code, I'm not getting a traceback error. The code is running but It doesn't look like it's checking the Point.

Here's a sample output of the program that I just ran...
--------
Now Let's play with two dice!
Press Enter to Roll the Dice once
The first die reads.. 6

The second die reads.. 5

The Point is now 11
Roll Again?
The first die reads.. 4

The second die reads.. 3

The Point is now 7
Roll Again?
The first die reads.. 2

The second die reads.. 3

The Point is now 5
Roll Again?
The first die reads.. 2

The second die reads.. 5

The Point is now 7
Roll Again?
The first die reads.. 3

The second die reads.. 5

The Point is now 8
Roll Again?
The first die reads.. 6

The second die reads.. 3

The Point is now 9
Roll Again?
digiwx9
 
Posts: 5
Joined: Wed Apr 16, 2014 11:43 am

Re: Help with Python Craps Simulator

Postby digiwx9 » Wed Apr 16, 2014 2:44 pm

I managed to update the code, but I"m still seeing the same issue :/
Code: Select all
from Dice import PairOfDice
global Val

def pointCheck():
    gamestatus = ''
    while gamestatus == "Continue":
        if myPoint == Val:
            gamestatus == "WON"
        elif myPoint == 7:
            gamestatus = "LOST"
        if gamestatus == "WON":
            print("Winner Winner")
        else:
            print("Seven Out, Sorry!")
            print("Play Again?")
def MainDouble():
    bDice = PairOfDice()
    pointValue = ''
    doubleDiceRoll =''
    global myPoint
    input("Pres Enter to Roll the Dice Once")

    while doubleDiceRoll == '':
        bDice.toss()
        print('The first die reads..' + str(bDice.getValue1()) + '\n')
        print('The second die reads..' + str(bDice.getValue2()) + '\n')

        Val = bDice.getTotal()

        if Val in [7,11]:
            gamestatus = "WON"
        elif Val in [2,3,12]:
            gamestatus = "LOST"
        else:
            gamestatus = "CONTINUE"
            myPoint = Val
            pointValue = myPoint
            print('The point is now..' + str(pointValue) + '\n')
            pointCheck()
            doubleDiceRoll = input("Roll Again?")
           

MainDouble()
digiwx9
 
Posts: 5
Joined: Wed Apr 16, 2014 11:43 am

Re: Help with Python Craps Simulator

Postby metulburr » Wed Apr 16, 2014 2:47 pm

We also need your full code. The fact that we dont have your dice class, makes it a pain to rerun your script and for us to get the same results as you.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1499
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help with Python Craps Simulator

Postby digiwx9 » Wed Apr 16, 2014 3:08 pm

metulburr wrote:We also need your full code. The fact that we dont have your dice class, makes it a pain to rerun your script and for us to get the same results as you.


Of course, my apologies.

Here is the code for the class. I need to keep this portion of the code the same.

Code: Select all

import random

class Die:
    def __init__(self, sideNum = 6): #default to 6-sided die
        self.sideNum = sideNum
        self.faceValue = sideNum

    def toss(self): #roll die and set face value
        self.faceValue = random.randint(1, self.sideNum)

    def getValue(self): #return die face value
        return self.faceValue
#
#
#
class PairOfDice:
    def __init__(self, sideNum = 6): #default to 6-sided dice
        self.sideNum = sideNum
        self.die1 = Die(self.sideNum)
        self.die2 = Die(self.sideNum)
       
    def toss(self): #have each die in pair of dice toss themselves
        self.die1.toss()
        self.die2.toss()

    def getValue1(self): #get value of die1
        return self.die1.getValue()

    def getValue2(self): #get value of die2
        return self.die2.getValue()

    def getTotal(self): #get sum of both dice
        return self.getValue1() + self.getValue2()
digiwx9
 
Posts: 5
Joined: Wed Apr 16, 2014 11:43 am

Re: Help with Python Craps Simulator

Postby stranac » Wed Apr 16, 2014 3:39 pm

In my opinion, classes are way overkill here.
Everything your two classes do could be done by a single(very simple) function.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1209
Joined: Thu Feb 07, 2013 3:42 pm

Re: Help with Python Craps Simulator

Postby metulburr » Wed Apr 16, 2014 3:56 pm

In my opinion, classes are way overkill here.
Everything your two classes do could be done by a single(very simple) function.

if your referring to the dice classes, i would have to agree.

I need to keep this portion of the code the same.

why cant you simplify it?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1499
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Help with Python Craps Simulator

Postby Mekire » Wed Apr 16, 2014 4:11 pm

metulburr wrote:why cant you simplify it?
Almost definitely part of his assignment description.
digiwx9's professor wrote: Use these two pointless classes to implement something that could be done much more simply.

Sounds like a 101 CS course to me.

-Mek
User avatar
Mekire
 
Posts: 1011
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Help with Python Craps Simulator

Postby digiwx9 » Wed Apr 16, 2014 4:18 pm

Finally, found the issue. Thanks MetulBurr!
digiwx9
 
Posts: 5
Joined: Wed Apr 16, 2014 11:43 am


Return to General Coding Help

Who is online

Users browsing this forum: liveact, Yoriz, ZeSta and 3 guests