Long Journey Through Unknown Part I

Imagine you’re a real humanist, you can speak some languages, you love reading books. Maths, physics were not your favourite at school, although you were able to solve some problems.
Moreover, you’re over thirty, you’re a dinosaur, and you dream of doing things you have never been doing before. You know nothing about coding, besides names of some programming languages: Pascal (your nightmare at high school), Python, C++, C#, Java. Only the names. You’re just off the boat. Hi! It’s me a couple weeks ago!

One day I decided to make my first big step, I was finally ready to start a basic Python programming course. In fact, my husband said it was easy, so I believed him. I definitely won’t make that mistake again XD. However, if you think I have a great developer at home who can teach me, you’re wrong. Of course, he will answer some of my question kindly, but if there’re not too many of them and only when I have a proof I’ve tried to find the answer myself many times.

OK, when a context is clear, I may begin.

During my course, I have learned some basics, and have written a couple of simple programs, using variables, conditions, loops, functions and simple user input. One of the task was to write a simple calculator.

You have three inputs: first number, second number (both are floats) and operation, which you can choose, and you have to use if-elif-else statements and remember about division by 0.

First, I wrote such “beautiful” program:

first_number = float(input())
second_number = float(input())
operation = input()


if operation == "+":
    print(first_number + second_number)
elif operation == "-":
    print(first_number - second_number)
elif operation == "/":
    if second_number == 0:
        print("Division by zero!")
    else:
        print(first_number / second_number)
elif operation == "*":
    print(first_number * second_number)
elif operation == "mod":
    if second_number == 0:
        print("Division by zero!")
    else:
        print(first_number % second_number)
elif operation == "pow":
    print(first_number ** second_number)
elif operation == "div":
    if second_number == 0:
        print("Division by zero!")
    else:
        print(first_number // second_number)

Then I noticed that division by zero check and message repeat three times, so I thought I could write a function. I wrote it, then I replaced the nested if-else statement with the function, and here it is:

first_number = float(input())
second_number = float(input())
operation = input()


def division_by_zero(second):
    if second == 0:
        print("Division by 0!")
        return True
    return False


if operation == "+":
    print(first_number + second_number)
elif operation == "-":
    print(first_number - second_number)
elif operation == "/":
    if not division_by_zero(second_number):
        print(first_number / second_number)
elif operation == "*":
    print(first_number * second_number)
elif operation == "mod":
    if not division_by_zero(second_number):
        print(first_number % second_number)
elif operation == "pow":
    print(first_number ** second_number)
elif operation == "div":
    if not division_by_zero(second_number):
        print(first_number // second_number)

I turned out that the division_by_zero function reduced the code, but didn’t remove the nested condition entirely, so I decided to change each operation into function, and I put the zero check into division, modulo and integer_division ones.

DIV_BY_ZERO = "Division by 0!"

# the calculator must just print the result.
# if we want more universal function, we have to change it


def add(a, b):
    print(a + b)


def subtraction(a, b):
    print(a - b)


def multiplication(a, b):
    print(a * b)


def division(a, b):
    print(a / b if b != 0 else DIV_BY_ZERO)


def integer_division(a, b):
    print(a // b if b != 0 else DIV_BY_ZERO)


def modulo(a, b):
    print(a % b if b != 0 else DIV_BY_ZERO)


def power(a, b):
    print(a ** b)


first_number = float(input())
second_number = float(input())
operation = input()

if operation == "+":
    add(first_number, second_number)
elif operation == "-":
    subtraction(first_number, second_number)
elif operation == "/":
    division(first_number, second_number)
elif operation == "*":
    multiplication(first_number, second_number)
elif operation == "mod":
    modulo(first_number, second_number)
elif operation == "pow":
    power(first_number, second_number)
elif operation == "div":
    integer_division(first_number, second_number)

This solution is not perfect, still we have repeated condition, but it’s shorter, and has removed the nested condition from main if-elif-else block. It looks like it’s a pretty good update. I even impressed my hubby, when I showed him the code.;)

At the end I decided to give some instructions for a user, and use loop so that the program is working constantly until the user writes 'exit'.

DIV_BY_ZERO = "Division by 0!"


def add(a, b):
    print(a + b)


def subtraction(a, b):
    print(a - b)


def multiplication(a, b):
    print(a * b)


def division(a, b):
    print(a / b if b != 0 else DIV_BY_ZERO)


def integer_division(a, b):
    print(a // b if b != 0 else DIV_BY_ZERO)


def modulo(a, b):
    print(a % b if b != 0 else DIV_BY_ZERO)


def power(a, b):
    print(a ** b)


while True:
    first_number = float(input(" ;u b
 Write a first number: "))
    second_number = float(input("Write a second number: "))
    operation = input("""
    Choose an operation. Write:
    + – sum,
    - subtract,
    / - division,
    * - multiplication,
    mod - modulo,
    pow - power,
    div - integer division
    exit - exit program
    """)
    if operation == "+":
        add(first_number, second_number)
    elif operation == "-":
        subtraction(first_number, second_number)
    elif operation == "/":
        division(first_number, second_number)
    elif operation == "*":
        multiplication(first_number, second_number)
    elif operation == "mod":
        modulo(first_number, second_number)
    elif operation == "pow":
        power(first_number, second_number)
    elif operation == "div":
        integer_division(first_number, second_number)
    elif operation == "exit":
        print("See you later, alligator!")
        break
    else:
        print("Wrong choice. Let's start again!")

Still, I can’t write any spectacular feature, but I’m happy I have made some effort, and I can write simple things such as: not a very complicated calculator, or a dictionary to which you can add words and definitions, you can delete them and check the meaning of a given word.

Post Scriptum

OK, OK I should have written about testing because a test is on its way on my software tester course, but who cares? (Hahaha!) Coding is so exciting!

Copyright © 2020 Agnieszka Pohl