How to Build a Simple Console Calculator in Python

 How to Build a Simple Console Calculator in Python

In this program, we will learn how create a basic console calculator in python to perform arithmetic operation of addition, subtraction, division and multiplication using if, elif, and else  statement

We will let the user to enter the first number followed by the arithmetic operator of his/her choice after which they enter the second number to perform the operation.

print('================================+ CONSOLE CALCULATOR +=================================')
print('-------------------------------------By: Petasco --------------------------------------')
# taking the first number from the user
num_1 = float(input('ENTER THE FIRST NUMBER: '))
print('--------------------------------------------')

# taking the operator from user
Operator = input("ENTER YOUR OPERATOR [+,-,*,/]: ")
print('--------------------------------------------')
# taking the second number from user
num_2 = float(input("ENTER THE SECOND NUMBER: "))
print('--------------------------------------------')
if Operator == '+':
sum = num_1 + num_2
print(num_1, '+', num_2, '=', sum)
elif Operator == '-':
diff = num_1 - num_2
print(num_1, '-', num_2, '=', diff)
elif Operator == '*':
pro = num_1 * num_2
print(num_1, 'x',num_2, '=', pro)
elif Operator == '/':
ans = num_1 / num_2
print(num_1, '/', num_2, '=', ans)
else:
print('ERROR!')

Now that we have finished creating the code, let's run the program

Addition (+)




Subtraction (-)





Division (/)



Multiplication (*)

This is how you can build a simple calculator in python to perform addition, subtraction, division and multiplication



Subscribe My YouTube Channel and Follow Me

YouTube

TikTok 

Twitter

Facebook 

Post a Comment

0 Comments