DEV Community

Oluwatoyin Ariyo
Oluwatoyin Ariyo

Posted on

100 Days of Code: Day 10

Day 10 of Angela Yu's Python bootcamp was about functions with outputs (using the return statement to exit a function and to return a value).

The main goal of today's project was to build a calculator that lets the user calculate two numbers by adding, subtracting, multiplying or dividing and then asking them if they want to continue calculating or start a new calculation. Here is the Python code:

import os
from art import logo


def cls():  # Cross-platform clear screen
    os.system('cls' if os.name == 'nt' else 'clear')


# Add
def add(n1, n2):
    return n1 + n2


# Subtract
def subtract(n1, n2):
    return n1 - n2


# Multiply
def multiply(n1, n2):
    return n1 * n2


# Divide
def divide(n1, n2):
    return n1 / n2


def calculator():
    print(logo)


operations = {
    "+": add,
    "-": subtract,
    "*": multiply,
    "/": divide
}

first_number = float(input("What's the first number? "))
second_number = float(input("What's the second number? "))
for sign in operations:
    print(sign)
    should_continue = True

while should_continue:
    operation_sign = input("Pick an operation from the line above: ")
    calculation_function = operations[operation_sign]
    answer = calculation_function(first_number, second_number)
    print(f"{first_number} {operation_sign} {second_number} = {answer}")
    if input("Type 'y' to continue calculating with {answer} or type 'n' to start a new calculation: ") == "y":
        first_number = answer
    else:
        should_continue = False
        cls()
        calculator()

calculator()
Enter fullscreen mode Exit fullscreen mode

I will continue with Day 11 tomorrow.

Top comments (0)