Classes and Objects
Four small exercises that build on each other — at the end you have a finished dice class. From part 2 on, a button copies your code from the previous part.
Part 1 — The class Wuerfel
A blueprint with one attribute.
Exercise
Write the class Wuerfel. The constructor __init__ receives the parameter anzahl_seiten with the default value 6 and stores it in the attribute seiten.
Example Wuerfel().seiten yields 6, Wuerfel(4).seiten yields 4.
Part 1 — Your Code
Loading Python…Tests
Part 2 — The method maxwert()
A first method that returns a value.
Exercise
Copy your class from part 1 and add the method maxwert(). It returns the number of sides of the dice — the highest value it can show.
Example Wuerfel(4).maxwert() yields 4.
Part 2 — Your Code
Loading Python…Tests
Part 3 — The method wuerfeln()
Random dice values with NumPy.
Exercise
Copy your class from part 2 and add the method wuerfeln(). It returns a random integer from 1 up to and including the dice's number of sides.
Note Import numpy at the very top and use numpy.random.randint(low, high)The lower bound is included and the upper bound is excluded. Use 1 as the lower bound and the number of sides plus 1 as the upper bound.
Part 3 — Your Code
Loading Python…Tests
Part 4 — Two dice together
Using two objects from one blueprint.
Exercise
Copy your class from part 3 and write the function below it augensumme(seed). It first calls numpy.random.seed(seed) , then builds a four-sided and a six-sided dice, rolls the four-sided one first, then the six-sided one, and returns the sum of both rolls.
Note numpy.random.seed(seed) makes randomness repeatable: with the same seed the same numbers come up. That is how the check can recompute your result.
Part 4 — Your Code
Loading Python…Tests
Report as PDF
Document and download your solutions.
Module overview
| Part | Status |
|---|---|
| Part 1: Blueprint | Open |
| Part 2: Method | Open |
| Part 3: Rolling | Open |
| Part 4: Two dice | Open |
Not all parts are complete yet — the report can still be created.
Download PDF
Enter your name. The PDF contains your code, completion status, and a QR code for verification.