Python: my first calculator (v1)

Gerson Marques
2 min readSep 21, 2020

My goal: to develop a simple calculator where the user will be able to inform 2 numbers and what operation he wants to do: sum, subtract, multiply or divide. Let's start!

Printing the title of my calculator:

print("\n******************* Gerson Python Calculator *******************")
print("\n")

Asking the first number to be calculated:

firstNumber = int(input("Please, inform the first number: "))
print("\n")

Asking which operation is going to be done:

print("Now inform what operation you want to do, according to the options below: ");
print("\n")

One for sum, two for subtract, 3 for multiply and four for divide:

print("1 - Sum");
print("2 - Subtract");
print("3 - Multiply");
print("4 - Divide");
print("\n")

Here, we get the operation

operation = int(input("What operation do want to do? "))
print("\n")

Asking the second number to be calculated:

secondNumber = int(input("Now, inform the second number: "))
print("\n")

Here is all the magic start to happen: we send our three parameters (operation, first number and second number) to the function ‘calculator’. In the variable ‘result’ will be the result:

result = calculator(operation, firstNumber, secondNumber)

Here is the function calculator:

This function does the math, it receive the parameters:'''
operation: 1 (+), 2(-), 3(*) or 4(/)
n1: the first number to be calculate
n2: the second number to be calculate
'''
def calculator(operation, n1, n2):
if operation == 1:
return 1 + 1
if operation == 2:
return n1 - n2
if operation == 3:
return n1 * n2

elif operation == 4:
return n1 / n2

Now all we have to do is inform the result to the user:

print("The result is %r" %(result))

See the calculator in action!

Python calculator, doing a math operation of 10 * 5

All the code, here!

Calculator function.
All the code

--

--