-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
88 lines (82 loc) · 3.67 KB
/
calculator.py
File metadata and controls
88 lines (82 loc) · 3.67 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# A simple calculator class that performs basic arithmetic operations and keeps a history of calculations in a file.
class Calculator:
def __init__(self):
with open("history.txt", "w") as f:
f.write("") # Clear history on initialization
self.history = []
def add(self, a, b):
result = a + b
with open("history.txt", "a") as f:
f.write(f"Added {a} and {b}: {result}\n")
self.history.append(f"Added {a} and {b}: {result}")
return result
def subtract(self, a, b):
result = a - b
with open("history.txt", "a") as f:
f.write(f"Subtracted {b} from {a}: {result}\n")
self.history.append(f"Subtracted {b} from {a}: {result}")
return result
def multiply(self, a, b):
result = a * b
with open("history.txt", "a") as f:
f.write(f"Multiplied {a} and {b}: {result}\n")
self.history.append(f"Multiplied {a} and {b}: {result}")
return result
def divide(self, a, b):
if b == 0:
self.history.append(f"Attempted to divide {a} by zero: Error")
return "Error: Division by zero"
result = a / b
with open("history.txt", "a") as f:
f.write(f"Divided {a} by {b}: {result}\n")
self.history.append(f"Divided {a} by {b}: {result}")
return result
def get_history(self):
return self.history
# Example usage
if __name__ == "__main__":
calc = Calculator()
print(calc.add(5, 3)) # Output: 8
print(calc.subtract(10, 4)) # Output: 6
print(calc.multiply(2, 7)) # Output: 14
print(calc.divide(20, 5)) # Output: 4.0
print(calc.divide(10, 0)) # Output: Error: Division by zero
print("\nHistory:")
for entry in calc.get_history():
print(entry)
print("\nHistory from file:")
with open("history.txt", "r") as f:
print(f.read())
# Interactive command loop to perform calculations and log history, also gives the option to clear history after each command.
while True:
command = input("Enter a command (add, subtract, multiply, divide) followed by two numbers: ")
parts = command.split()
with open("history.txt", "a") as f:
f.write(f"Command entered: {command}\n")
if len(parts) == 3:
operation, num1, num2 = parts[0], float(parts[1]), float(parts[2])
if operation == "add":
f.write(f"Performing addition: {num1} + {num2} = {calc.add(num1, num2)}\n")
print(calc.add(num1, num2))
elif operation == "subtract":
f.write(f"Performing subtraction: {num1} - {num2} = {calc.subtract(num1, num2)}\n")
print(calc.subtract(num1, num2))
elif operation == "multiply":
f.write(f"Performing multiplication: {num1} * {num2} = {calc.multiply(num1, num2)}\n")
print(calc.multiply(num1, num2))
elif operation == "divide":
f.write(f"Performing division: {num1} / {num2} = {calc.divide(num1, num2)}\n")
print(calc.divide(num1, num2))
else:
print("Unknown operation")
else:
print("Invalid command format")
clear_history = input("Do you want to clear the history? (yes/no): ")
if clear_history.lower() == "yes":
with open("history.txt", "w") as f:
f.write("")
f.close()
continue_input = input("Do you want to continue? (yes/no): ")
if continue_input.lower() != "yes":
print("Goodbye! See you next time.")
break