RE: Python lists and Math

  • From: BlueScale <bluescale1976@xxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sun, 19 Oct 2008 09:37:01 -0400

Hi,
I didn't get it to work.  Classes and OO in general have always given me
problems.  After working with Python a little, I have gotten somewhat
better but not much.  I did this in two files to get some practise with
import.  Import works fine, but I may have gone about the class thing
completely wrong.  My code has changed slightly from what I posted
earlier.  So, here's my code for each file:
#dice.py
class dice:
 def __init__(self,numDice):
  self.numDice = numDice

 def roll(self):
  import random
  die = []
  x = 0
  while x < self.numDice:
   die.append(random.randint(1,6))
   x += 1
  return die

#dice-test.py
#!/usr/bin/python
import dice
score = 0
roll = dice.dice(1)
cast = roll.roll()
score += int(cast)
print score




On Sun, 2008-10-19 at 08:15 -0500, Ken Perry wrote:
> You didn't exactly send enough of the code for someone to answer this
> if they had not ran into the problem.  I actually just wrote a dice
> class recently for a project I am working on.  I am betting because of
> the error message you got the roll is a list if this is the case its
> probably a list of your dice so what you should have had was
>  
> score=roll[i].roll(6)
>  
> i being the number of the die you want to roll for example if you have
> a list of 5 dice for a game like Yahtzee you could do something like
>  
> for i in range (0,5):
>   score+=roll[i].roll(6)  
>  
> If you can't get it to work let me know I can take your code and fix
> it probably in a bout 2 minutes or less.  
>  
> Ken
>  
> y
>  
> 
> 
> 
> 
> ______________________________________________________________________
> From: programmingblind-bounce@xxxxxxxxxxxxx
> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of BlueScale
> Sent: Sunday, October 19, 2008 7:40 AM
> To: programmingblind@xxxxxxxxxxxxx
> Subject: Python lists and Math
> 
> 
> 
> 
> Hi,
> In my effort to learn Python, I have written a dice class.  I have a
> roll method that accepts one argument, the number of dice to roll.  It
> returns a list of the results.  In the program I wrote to test the
> class, I have something like:
> score = roll.roll(1)
> This rolls 1 six sided die.  The problem I run in to is when I try to
> do:
> score += roll.roll(1)
> I get an error about the += operand not supporting list and int.  So
> my next move was to try:
> score += int(roll.roll())
> Which Python says can't be done because the number is stored in a
> list, not a string.  So, my question is, how do I keep track of the
> score?  IS there some function to convert lists to int?
> Thanks for the help
> 

Other related posts: