-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
49 lines (38 loc) · 1.25 KB
/
calculator.py
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
# calculator.py
print("\n WELCOME TO SIMPLE CALCULATOR \n")
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b == 0:
return "Error: Division by zero."
return a / b
def calculator():
while True:
print("Options: add, subtract, multiply, divide, exit \n")
operation = input("/n Choose an operation: ").lower()
if operation == 'exit':
break
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input, please enter numbers only.")
continue
if operation == 'add':
print(f"Result: {add(num1, num2)}")
elif operation == 'subtract':
print(f"Result: {subtract(num1, num2)}")
elif operation == 'multiply':
print(f"Result: {multiply(num1, num2)}")
elif operation == 'divide':
print(f"Result: {divide(num1, num2)}")
else:
print("Invalid operation, try again.")
if __name__ == "__main__":
calculator()
print("\nDeveloped By : IRFAN ULLAH KHAN")
print("@2024")