DEV Community

SAINAPEI LENAPUNYA
SAINAPEI LENAPUNYA

Posted on

Day 7: Simple Calculator - Putting It All Together - 30 Days of Python Challenge

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())
Enter fullscreen mode Exit fullscreen mode

Breaking It Down 🔍

Let me explain this step by step:

  1. def calculator(): - This creates a function named calculator. Functions are reusable blocks of code. The def keyword means "define." Think of it as creating your own custom command!

  2. 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!

  3. op = input("Operator (+ - * /): ") - We ask the user which operation they want to perform.

  4. b = float(input("Second number: ")) - Getting the second number, also as a float.

  5. if op == "+": - Checking which operator was entered. If it's addition, we return the sum.

  6. return a + b - The return keyword sends the result back. This is how functions give us answers!

  7. The other operations - Similar checks for subtraction, multiplication, and division.

  8. return a / b if b != 0 else "Cannot divide by zero" - This is a conditional expression! It checks if b is not zero before dividing. If it is zero, we return an error message. Smart, right?

  9. return "Invalid operator" - If the user enters something other than +, -, *, or /, we return this message.

  10. 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
Enter fullscreen mode Exit fullscreen mode

Or if you try division:

First number: 20
Operator (+ - * /): /
Second number: 4
Result: 5.0
Enter fullscreen mode Exit fullscreen mode

And it even handles errors:

First number: 10
Operator (+ - * /): /
Second number: 0
Result: Cannot divide by zero
Enter fullscreen mode Exit fullscreen mode

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
  • return sends 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)