-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy path5_project_smart_calculator.py
More file actions
62 lines (49 loc) · 1.61 KB
/
Copy path5_project_smart_calculator.py
File metadata and controls
62 lines (49 loc) · 1.61 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
# ==========================================
# SECTION 5: Day 5 Project - The Smart Calculator
# ==========================================
print("SECTION 5: Day 5 Project - The Smart Calculator")
print("-" * 50)
# This project uses functions to create a clean and organized calculator.
# --- Function Definitions ---
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! Cannot divide by zero."
else:
return a / b
# --- Main Program Loop ---
while True:
print("\n--- Calculator Menu ---")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Quit")
choice = input("Enter your choice (1-5): ")
if choice in ["1", "2", "3", "4"]:
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter numbers only.")
continue
if choice == "1":
print(f"Result: {add(num1, num2)}")
elif choice == "2":
print(f"Result: {subtract(num1, num2)}")
elif choice == "3":
print(f"Result: {multiply(num1, num2)}")
elif choice == "4":
print(f"Result: {divide(num1, num2)}")
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice. Please enter a number between 1 and 5.")
print("\n" + "="*50)
# print("Congratulations on completing Day 5! You can now write clean, reusable code with functions.")