An Intro to
Programming
with Python

Fall 2006
course
navigation

oct 25

# Remember: myCircle = Circle( Point(0,0), 20) myCircle.draw(win) myCircle.move(dx, dy) ... # msdie.py # Class definition for an n-sided die from random import randint class MSDie: "Simulates a multi-sided dice.""" def __init__(self, sides): self.sides = sides self.value = 1 def roll(self): self.value = randint(1,self.sides) def getValue(self): return self.value def setValue(self, value) self.value = value # if class definition is in another file msdie.py from msdie import MSDie die1 = MSDie(6) # create a new 6-sided die die1.roll() # roll it. print "You rolled a", die1.value # access its value # essentially these two calls are the same : roll(die1) die1.roll() # inside the roll() method, #'self' will be the 'die1' object during either of these last two.
http://cs.marlboro.edu/ courses/ fall2006/python/ notes/ oct_25
last modified Wednesday October 25 2006 1:43 am EDT

attachments [paper clip]

     name last modified size
   craps.py Oct 25 2006 1:24 am 4.48kB