Welcome Back to Day 7!
Hey everyone! It's Day 7 of my 30 Days of Python Challenge, and today is EXTRA exciting because we're building our first real project a calculator!
If you missed the previous days:
- [Day 1: Print Statements]
- [Day 2: Variables and Data Types]
- [Day 3: Type Casting]
- [Day 4: User Input]
- [Day 5: Arithmetic Operators]
- [Day 6: If Statements]
Today, we're combining everything we've learned into a functional calculator. Let's build something awesome!
Day 7: Simple Calculator - Our First Real Project
Today's mission: Build a Calculator. This is where all our previous lessons come together! We're using user input, type casting, arithmetic operators, if statements, and introducing something new—functions!
What I Learned
Building this calculator taught me:
- How to organize code into reusable functions
- How to handle user input for calculations
- How to validate operations and prevent errors
- How all the concepts connect to create something functional
This feels like a huge milestone! 🎯
My Code
Here's what I wrote for Day 7:
# Day 7 - Calculator
def calculator():
a = float(input("First number: "))
op = input("Operator (+ - * /): ")
b = float(input("Second number: "))
if op == "+":
return a + b
if op == "-":
return a - b
if op == "*":
return a * b
if op == "/":
return a / b if b != 0 else "Cannot divide by zero"
return "Invalid operator"
print("Result:", calculator())
Breaking It Down 🔍
Let me explain this step by step:
def calculator():- This creates a function namedcalculator. Functions are reusable blocks of code. Thedefkeyword means "define." Think of it as creating your own custom command!a = float(input("First number: "))- We get the first number from the user and convert it to a float (decimal). Remember type casting from Day 3? Here it is in action!op = input("Operator (+ - * /): ")- We ask the user which operation they want to perform.b = float(input("Second number: "))- Getting the second number, also as a float.if op == "+":- Checking which operator was entered. If it's addition, we return the sum.return a + b- Thereturnkeyword sends the result back. This is how functions give us answers!The other operations - Similar checks for subtraction, multiplication, and division.
return a / b if b != 0 else "Cannot divide by zero"- This is a conditional expression! It checks ifbis not zero before dividing. If it is zero, we return an error message. Smart, right?return "Invalid operator"- If the user enters something other than +, -, *, or /, we return this message.print("Result:", calculator())- This calls (runs) our function and prints the result!
Output
When you run this code, you'll see something like:
First number: 15
Operator (+ - * /): +
Second number: 7
Result: 22.0
Or if you try division:
First number: 20
Operator (+ - * /): /
Second number: 4
Result: 5.0
And it even handles errors:
First number: 10
Operator (+ - * /): /
Second number: 0
Result: Cannot divide by zero
New Concepts Introduced🎯
Functions:
- Defined with
def function_name(): - Can take inputs (parameters) and give outputs (return values)
- Make code reusable and organized
- Called by writing the function name followed by parentheses
Return Statement:
- Sends a value back from the function
- Ends the function execution
- The returned value can be used elsewhere (like in our print statement)
Error Handling:
- We prevent division by zero with a conditional check
- We validate the operator to ensure it's one we support
Connecting the Dots 🔗
Look at how everything we've learned comes together:
- Day 1 (Print): Used to display the result
- Day 2 (Variables): Store numbers and operators
- Day 3 (Type Casting): Convert input to floats
- Day 4 (User Input): Get numbers and operator from user
- Day 5 (Arithmetic): Perform the actual calculations
- Day 6 (If Statements): Choose which operation to perform
This is the power of programming—building blocks that create something bigger!
Key Takeaways💡
- Functions organize code into reusable blocks using
def -
returnsends values back from functions - You can combine multiple if statements to handle different cases
- Always validate user input (like checking for division by zero)
- Conditional expressions (
value if condition else other_value) provide compact if-else logic - Real programs combine multiple concepts to solve problems
- Building projects reinforces everything you've learned!
What's Next?
Tomorrow on Day 8, I'll be building a weight converter! We'll create another practical tool that converts between different units. More real-world programming ahead! ⚖️
Let's Connect!💬
I'd love to hear from you!
- How does it feel to build your first project?
- What other calculators or tools do you want to build?
- Did the function concept click for you?
Drop a comment below! If you're coding along, try adding more operators (maybe exponentiation or modulus) and share your enhanced calculator!
Don't forget to follow me for daily updates. Day 8 continues the project streak!
Happy Coding!


Top comments (0)