🎉
Congratulations!
You've made it to the final day! Today you'll build a complete calculator using everything you've learned.
📋 Project Overview
Your calculator should:
- Take two numbers as input
- Ask which operation (+, -, *, /)
- Perform the calculation
- Handle errors (like division by zero)
- Let the user do multiple calculations
💻 The Calculator Code
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!"
return a / b
def calculator():
print("🧮 Simple Calculator")
print("Operations: +, -, *, /")
print("Type 'quit' to exit\n")
while True:
operation = input("Operation: ")
if operation == "quit":
print("Goodbye! 👋")
break
try:
num1 = float(input("First number: "))
num2 = float(input("Second number: "))
except ValueError:
print("❌ Please enter valid numbers!\n")
continue
if operation == "+":
print(f"Result: {add(num1, num2)}\n")
elif operation == "-":
print(f"Result: {subtract(num1, num2)}\n")
elif operation == "*":
print(f"Result: {multiply(num1, num2)}\n")
elif operation == "/":
print(f"Result: {divide(num1, num2)}\n")
else:
print("❌ Invalid operation! Use +, -, *, or /\n")
calculator()
📖 How It Works
- Functions - We created separate functions for each operation
- While Loop - Keeps the calculator running until user quits
- Try/Except - Handles invalid number inputs gracefully
- If/Elif/Else - Chooses which operation to perform
- F-strings - Formats the output nicely
💡 Try This: Add more operations like power (**), modulo (%), or floor division (//). Can you add a history feature that remembers past calculations?
🚀 Challenge Extensions
Want to make it even better? Try these:
- History: Store all calculations in a list and show them on exit
- GUI: Use Python's
tkinter library for a graphical calculator
- Scientific: Add sqrt, sin, cos using the
math module
- Save/Load: Save calculation history to a file
🐍
You Did It!
You've completed all 7 days of One Week Python!
What You Learned:
Day 1: print()
Day 2: Variables
Day 3: If/Else
Day 4: Functions
Day 5: Lists
Day 6: Files
Day 7: Project!
This is just the beginning. Keep coding, keep building, and most importantly - have fun! 🎉
You've reached 100%!