aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / svc / tests / fixtures / code / short.txt
| 1 | Here is a small calculator you can run with python3 calc.py. |
| 2 | |
| 3 | ```python |
| 4 | # calc.py: a small calculator that reads one sum at a time. |
| 5 | import operator |
| 6 | |
| 7 | OPS = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv} |
| 8 | |
| 9 | |
| 10 | def calculate(a, op, b): |
| 11 | if op not in OPS: |
| 12 | raise ValueError("unknown operator " + op) |
| 13 | if op == "/" and b == 0: |
| 14 | raise ZeroDivisionError("cannot divide by zero") |
| 15 | return OPS[op](a, b) |
| 16 | |
| 17 | |
| 18 | def main(): |
| 19 | print("Type a sum like 3 + 4, or q to quit.") |
| 20 | while True: |
| 21 | line = input("> ").strip() |
| 22 | if line in ("q", "quit"): |
| 23 | break |
| 24 | try: |
| 25 | a, op, b = line.split() |
| 26 | print(calculate(float(a), op, float(b))) |
| 27 | except (ValueError, ZeroDivisionError) as e: |
| 28 | print("Sorry:", e) |
| 29 | |
| 30 | |
| 31 | if __name__ == "__main__": |
| 32 | main() |
| 33 | ``` |