Methods and Objects in Action
Four exercises: first a measurement series that calculates and introduces itself, then the dice simulation and a pressure sensor. Part 2 copies the code from part 1 with a button.
Part 1 — The class Messreihe
A list as an attribute and a method that calculates.
Exercise
Write the class Messreihe. The constructor receives name and messwerte and stores both in the attributes name and messwerte — the readings as its own list. The method mittelwert() returns the mean of the readings.
Example Messreihe("Druck", [1, 2, 3]).mittelwert() yields 2.0.
Part 1 — Your Code
Loading Python…Tests
Part 2 — The measurement series introduces itself
__str__ for readable output.
Exercise
Copy your class from part 1 and add the method __str__(). It returns a text that states the name, the number of values and the mean.
Example print(Messreihe("Druck", [1, 2, 3])) shows exactly Messreihe Druck: 3 Werte, Mittelwert 2.0.
Part 2 — Your Code
Loading Python…Tests
Part 3 — Simulating a pair of dice
Objects in a loop, running mean.
Exercise
The finished dice class from the experiment “Classes and Objects” is already in the editor. Below it, write the function simuliere_wuerfelpaar(anzahl, seed=0): it first calls numpy.random.seed(seed) , builds a four-sided and a six-sided dice and in every pass rolls the four-sided one first, then the six-sided one. It returns a list that, after each pass, contains the mean of all sums so far.
Example The first entry is the first sum; the second entry is the mean of the first two sums. For anzahl = 0 the list is empty.
Part 3 — Your Code
Loading Python…Tests
Part 4 — A pressure sensor that checks readings
A method that decides.
Exercise
Write the class Drucksensor(name, minimum, maximum, einheit="bar") and store all four values as attributes. The method ist_plausibel(wert) gibt True if the value lies between minimum and maximum, limits included. spanne() returns the difference between maximum and minimum, and __str__() introduces the sensor.
Example print(Drucksensor("P1", 0, 10)) shows Drucksensor P1: 0 bis 10 bar.
Part 4 — Your Code
Loading Python…Tests
Report as PDF
Document and download your solutions.
Module overview
| Part | Status |
|---|---|
| Part 1: Measurement series | Open |
| Part 2: Output | Open |
| Part 3: Simulation | Open |
| Part 4: Pressure sensor | 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.