0% found this document useful (0 votes)
354 views

12/7/2011 CS Using Die Object

This document introduces an assignment to use an MSDie class representing a multi-sided die. The MSDie class is provided and defines methods to initialize the die with a given number of sides, roll it randomly, get its value, and set its value. The assignment asks the student to write two functions outside the class: makeRandomCode() that uses an MSDie object to choose random numbers, and rollsUntilSeven() that rolls two dice in a loop until their values add up to 7.

Uploaded by

Matt Carlberg
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
354 views

12/7/2011 CS Using Die Object

This document introduces an assignment to use an MSDie class representing a multi-sided die. The MSDie class is provided and defines methods to initialize the die with a given number of sides, roll it randomly, get its value, and set its value. The assignment asks the student to write two functions outside the class: makeRandomCode() that uses an MSDie object to choose random numbers, and rollsUntilSeven() that rolls two dice in a loop until their values add up to 7.

Uploaded by

Matt Carlberg
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Introduction to Computer Science & Programming

12/7/2011
Matthew Carlberg

Using Objects

In this assignment, you will use an object representing a multi-sided die. The MSDie
class deIinition is shown below and can be Iound on Dropbox ~ Python Resources ~ Unit
4 Code ~ MSDie.py. You DO NOT have to change the class deIinition in any way Ior
this homework.

Irom random import randrange

class MSDie:

deI init(selI, sides):
selI.sides sides
selI.value 1

deI roll(selI):
selI.value randrange(1,selI.sides1)

deI getValue(selI):
return selI.value

deI setValue(selI, value):
selI.value value

Assignment: Your job is to add two Iunctions AFTER (outside oI) the class deIinition
that use MSDie objects. In each Iunction, you must create at least one instance oI an
MSDie object.

1. makeRandomCode(n, m). Exact same implementation as your project
makeRandomCode, except using an MSDie object to choose your random
numbers.

2. rollsUntilSeven(). This Iunction rolls TWO dice at a time, counting how many
rolls are required until the two dice ADD UP TO 7. You will need a while loop to
do this. (http://docs.python.org/tutorial/introduction.html#Iirst-steps-towards-
programming has a good example oI a while loop)

You might also like