In this simple python program, we have to make a calculator. It's a beginner-level python program.
To understand this example, you should have knowledge of the following Python programming topics:
In this python program, we have to make a simple calculator. The calculator is used to do Arithmetic operations in Pythonsuch as Addition, Subtraction, Multiplication, and Division. In this python program, we are adding, subtraction, multiplication, and division using user-defined functions. For that, we use def to define functions such as add, subtract, multiply, and divide. We give the user to select the choice of operation using an if condition in python and pass the parameters to the respective function and return the result from the function and print the result using print
statement in python.
STEP 1: Print the simple operations such as addition, subtraction, multiplication, division, and accept the input from the user using the input function in python programming and save it to the choice.
STEP 2: Accept the two numbers from the user and convert that to an integer using the int() function.
STEP 3: Use an if statement to check if the choice is 1, and if the condition got satisfied, it would call the function to add and pass the numbers as parameters and print the return value.
STEP 4: Use the elif
statement to check choice is 2 and if so, call the function to subtract and pass the two numbers as parameters and print the return value.
STEP 5: Use the elif
statement to check the choice is 3, and if so, call the function multiply and pass the two numbers as parameters and print the return value.
STEP 6: Use the elif
statement to check the choice is 4, and if so, call the function called divide and pass the parameters and print the return value.
STEP 7: Use the else statement as print invalid input. Use the def to define the functions, add, subtract, multiply, and divide and pass the program's parameter and return the result of the corresponding operation in the function.
STEP 1: Add the parameters x and y and return the result to the main program.
STEP 1: Subtract the parameters x any y return the result.
STEP 1: Multiply the parameters x and y and return the result.
STEP 1: Divide the parameters x and y and return the result.
def add(x, y):
"""This function adds two numbers"""
return x + y
def subtract(x, y):
"""This function subtracts two numbers"""
return x - y
def multiply(x, y):
"""This function multiplies two numbers"""
return x * y
def divide(x, y):
"""This function divides two numbers"""
return x / y
# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
Select Operation 1. Add 2. Subtract 3. Multiply 4. Divide Enter the choice 1/2/3/4 1 Enter first number : 4 Enter second number : 7 4 + 7 = 11