-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_calculator.py
More file actions
34 lines (27 loc) · 981 Bytes
/
python_calculator.py
File metadata and controls
34 lines (27 loc) · 981 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
This is a simple calculator script that supports basic operations.
"""
import os
from operator import add, sub, mul
def calc():
"""Perform calculation based on environment variables."""
operation = os.getenv("OPERATOR", "+") # Default to addition if no operator provided
num1 = int(os.getenv("NUMBER1", "0"))
num2 = int(os.getenv("NUMBER2", "0"))
operators = {'+': add(num1, num2), '-': sub(num1, num2), '*': mul(num1, num2)}
if operation in operators:
print(f'{num1} {operation} {num2} = {operators[operation]}')
else:
print(f"Invalid operator: {operation}")
def menu():
"""Handles menu using environment variable."""
choice = os.getenv("CHOICE", "1").lower() # Default to option 1 (calculation)
if choice == '1':
calc()
return True
if choice == 'q': # Change "elif" to "if"
return False
print(f'Invalid choice: {choice}')
return False
if __name__ == '__main__':
menu()