In this post we will see how to build/code a simple calculator…
we will go now step by step with an proper explanation of code and after some time we will also make a section were you will found the bunch of complete codes soon..
Now let’s begin…

Calculator
# first we create functions for doing proper operations as per the user required...
# Later on we will also discuss the what are functions and modules in python
def add(): # this is the method how we declared a function in python
x = float(input('Enter Values: ')) # this typecasting works with integers and decimals both
y = float(input('Enter Values: '))
return x + y
def subtract():
x = float(input('Enter Values: '))
y = float(input('Enter Values: '))
return x - y
def multiply():
x = float(input("Enter Values: "))
y = float(input("Enter Values: "))
return x * y
def divide():
x = float(input("Enter Values: "))
y = float(input("Enter Values: "))
return x / y
#Now make these in a separate new python file name that as "functions" name the file with your choice also if you want...
# Save this file in your current project directory
# Now it's time to declare the major function
import os
def Calculator():
print('1. Add')
print('2. Subtract')
print('3. Multiply')
print('4. Divide')
Input = int(input('Choose Your Option: ')) # " Input " is variable and " input() " is function remember they are different not same
if Input == 1:
os.system('cls') # this function used only for clear the screen
print("You Choose Addition Method...")
result = add()
print('Result: ',result)
if Input == 2:
os.system('cls') # this function used only for clear the screen
print("You Choose Subtraction Method...")
result = subtract()
print('Result: ',result)
if Input == 3:
os.system('cls') # this function used only for clear the screen
print("You Choose Multiplication Method...")
result = multiply()
print('Result: ',result)
if Input == 4:
os.system('cls') # this function used only for clear the screen
print("You Choose Division Method...")
result = divide()
print('Result: ',result)
else:
os.system('cls')
print('Thanks using this calculator....')
Calculator()
# now save this file as ' main.py ' or 'Give whatever name you want...'
Now we have two different files in your current directory or project folder i.e
- main.py
- functions.py
Now we see how we use functions written in functions.py file inside the main.py
Note:- Don’t excute the functions.py file by your self you just need to run main.py file and it willl autmatically run your functions.py file
Now you just need to Re-Edit your main.py file
# Now it's time to declare the major function
import functions
import time
import os
def Calculator():
print('Wellcome to Calculator...!')
print('1. Add')
print('2. Subtract')
print('3. Multiply')
print('4. Divide')
Input = int(input('Choose Your Option: ')) # " Input " is variable and " input() " is function remember they are different not same
if Input == 1:
os.system('cls') # this function used only for clear the screen
print("You Choose Addition Method...")
result = functions.add()
print('Result: ',result)
time.sleep(8) # this function is used here to pause the program for some time
if Input == 2:
os.system('cls') # this function used only for clear the screen
print("You Choose Subtraction Method...")
result = functions.subtract()
print('Result: ',result)
time.sleep(8)
if Input == 3:
os.system('cls') # this function used only for clear the screen
print("You Choose Multiplication Method...")
result = functions.multiply()
print('Result: ',result)
time.sleep(8)
if Input == 4:
os.system('cls') # this function used only for clear the screen
print("You Choose Division Method...")
result = functions.divide()
print('Result: ',result)
time.sleep(8)
else:
os.system('cls')
print('Thanks using this calculator....')
Calculator()
In the main.py file enter the steps written below…
1.import functions # Enter this syntax below the import os line
2. Re-Write the line result = add() to result = functions.add()
3. Re-Write the line result = subtract() to result = functions.subtract()
4. Re-Write the line result = multiply() to result = functions.multiply()
5. Re-Write the line result = divide() to result = functions.divide()
6. Also import time module
7. write syntex after the ‘ time.sleep(8) ‘ after the ‘ print(‘Result: ‘,result) ‘ line
Now all set execute the main.py file your calculator will work fine
You will also share your project ideas with us…
If you found any queries you will ask in comment section or contact us on whatsapp number mentioned
Thank you guys…
Keep Supporting…!
Leave a Reply