Recap: Airlock Control
Put together raise, your own exceptions, and else and finally
Mission: Airlock Control
The airlock opens only when the pressure inside is safe: from 90 to 110. The pressure arrives as text from a sensor that sometimes sends noise. After every try, safe or not, the airlock runs its seal check
- Write a class AirlockError that inherits from
Exception, and a class PressureError that inherits from it.PressureErrortakes the pressure, keeps it aspressure, and has the messagepressureand the pressure andout of range, likepressure 85 out of range - Write a function open_airlock(text) that turns the text into a whole number, raises
PressureErrorwhen the number is below 90 or above 110, and returnsopenedotherwise. A text that isn't a number raises Python's ownValueError, which the function leaves alone - Start your program with a variable readings, a list of strings. For example
readings = ["100", "85", "abc", "110"] - For each reading, call
open_airlock()insidetry. Print the reading,:and:- on a
PressureError, its message - on a
ValueError,not a number - in
else, what the function returned
- on a
- In
finally, count the seal checks. At the end, printOpened:and how many times the airlock opened,Seal checks:and how many checks ran, andFarthest:and the pressure of thePressureErrorfarthest from 100, ornoneif there was none. If two are as far, take the first
readings has from 1 to 10 readings
Example for the values above
100: opened
85: pressure 85 out of range
abc: not a number
110: opened
Opened: 2
Seal checks: 4
Farthest: 85