Python

Python Program to Make a Simple Calculator2 min read

Write a program to Make a Simple Calculator in Python

Code:




This Python program is a simple calculator that can perform four basic arithmetic operations: addition, subtraction, multiplication, and division. The program defines four functions, one for each operation: add(x, y), subtract(x, y), multiply(x, y), and divide(x, y). Each function takes two numbers as arguments and returns the result of the corresponding operation.

The main program starts by prompting the user to select an operation from a menu of four options (1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division). It then prompts the user to enter two numbers and assigns them to the variables num1 and num2.

The program uses an if-elif block to check the value of the variable choice, which represents the user’s selection. Depending on the value of choice, the program calls the corresponding function and passes num1 and num2 as arguments. The result is then printed out.

For example, if the user selects option 2 (subtraction), enters the numbers 150 and 50, the output would be:

Leave a Comment