Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# calculator.py
|
| 2 |
+
|
| 3 |
+
def add(x, y):
|
| 4 |
+
"""Adds two numbers."""
|
| 5 |
+
return x + y
|
| 6 |
+
|
| 7 |
+
def subtract(x, y):
|
| 8 |
+
"""Subtracts two numbers."""
|
| 9 |
+
return x - y
|
| 10 |
+
|
| 11 |
+
def multiply(x, y):
|
| 12 |
+
"""Multiplies two numbers."""
|
| 13 |
+
return x * y
|
| 14 |
+
|
| 15 |
+
def divide(x, y):
|
| 16 |
+
"""Divides two numbers. Handles division by zero."""
|
| 17 |
+
if y == 0:
|
| 18 |
+
return "Error! Division by zero."
|
| 19 |
+
return x / y
|
| 20 |
+
|
| 21 |
+
def main_calculator():
|
| 22 |
+
"""Main function to run the interactive calculator."""
|
| 23 |
+
print("Welcome to the Simple Python Calculator!")
|
| 24 |
+
print("Select operation:")
|
| 25 |
+
print("1. Add")
|
| 26 |
+
print("2. Subtract")
|
| 27 |
+
print("3. Multiply")
|
| 28 |
+
print("4. Divide")
|
| 29 |
+
print("5. Exit")
|
| 30 |
+
|
| 31 |
+
while True:
|
| 32 |
+
choice = input("Enter choice(1/2/3/4/5): ")
|
| 33 |
+
|
| 34 |
+
if choice in ('1', '2', '3', '4'):
|
| 35 |
+
try:
|
| 36 |
+
num1 = float(input("Enter first number: "))
|
| 37 |
+
num2 = float(input("Enter second number: "))
|
| 38 |
+
except ValueError:
|
| 39 |
+
print("Invalid input. Please enter numbers only.")
|
| 40 |
+
continue
|
| 41 |
+
|
| 42 |
+
if choice == '1':
|
| 43 |
+
print(f"{num1} + {num2} = {add(num1, num2)}")
|
| 44 |
+
elif choice == '2':
|
| 45 |
+
print(f"{num1} - {num2} = {subtract(num1, num2)}")
|
| 46 |
+
elif choice == '3':
|
| 47 |
+
print(f"{num1} * {num2} = {multiply(num1, num2)}")
|
| 48 |
+
elif choice == '4':
|
| 49 |
+
result = divide(num1, num2)
|
| 50 |
+
print(f"{num1} / {num2} = {result}")
|
| 51 |
+
elif choice == '5':
|
| 52 |
+
print("Exiting calculator. Goodbye!")
|
| 53 |
+
break
|
| 54 |
+
else:
|
| 55 |
+
print("Invalid input. Please enter a valid choice (1/2/3/4/5).")
|
| 56 |
+
print("-" * 30) # Separator for next operation
|
| 57 |
+
|
| 58 |
+
if __name__ == "__main__":
|
| 59 |
+
main_calculator()
|