2 Way To Make Python Calculator [ Complete Guide ]
python calculator using python program
the calculator in python programming seems very difficult.
we'll show you the easiest methods of creating program in python 2, 3 or 3.7
To make a simple python calculator, you need a basic understanding of python programming knowledge.
before start the coding of a calculator you must know about python syntax, function, variable, and conditions. i show in this post both methods of making a python calculator.
1. with function
2. without function
if you don't know well knowledge about python programming, don't worry we will show the easiest way of making and understanding the calculator made by python.
python program to make calculator [ without using function ]
this is a program of the calculator in python without using the function. it is a simplest form of python calculator.
#print options on screen
print("1.addition")
print("2.subtraction")
print("3.multiplication")
print("4.division")
#take input from user
choiceofuser=input("enter your option 1 to 4")
num=int(input("enter a number"))
num2=int(input("enter another number"))
#main calculation
if choiceofuser == '1':
print("addition is",num+num2)
elif choiceofuser == '2':
print("subtraction is",num-num2)
elif choiceofuser == '3':
print("multiplication is",num*num2)
elif choiceofuser == '4':
print("division is",num/num2)
#code ended
copy the code or write it manually in python shell to use a calculator.
output:
in this picture, all operation is performed addition, subtraction, multiplication, and division.
python program to make calculator [ using function ]
#function for add two numbers
def add(x, y):
return x + y
#function for subtract two numbers
def sub(x, y):
return x - y
#function for multiply two numbers
def mul(x, y):
return x * y
#function for divid two numbers
def div(x, y):
return x / y
#print options on screen
print("choos any one operational options")
print("1.addition")
print("2.subtraction")
print("3.multiplication")
print("4.division")
#User input
choice = input("Enter number of option")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print("addtion is:", add(num1,num2))
elif choice == '2':
print("subtraction is:", sub(num1,num2))
elif choice == '3':
print("multiplication is", mul(num1,num2))
elif choice == '4':
print("division is:", div(num1,num2))
else:
print("please enter any on 1 to 4")
#code ended
see the screenshot of the following code as working in python shell
output:
that's all. I showed the two simplest methods of python calculator. use the code in python shell for using and calculation numbers using python.
Comments
Post a Comment