This is a python program to make a calculator. We simply take two numbers and a choice of operation from the user and display the output.
To understand this example, you should have the knowledge of following topics in Python-
- User input
- Conditional Statements.
- User defined functions.
Lets get started!
Example Program
num1 = int(input("Please enter the first number: "))
num2 = int(input("Please enter the second number: "))
choice = input("Please enter choice for operation. + is for addition, - is for subtraction, * is for multiplication and / is for division: ")
res = "";
if choice == '+':
res = num1 + num2
elif choice == '-':
res = num1 - num2
elif choice == '*':
res = num1 * num2
elif choice == '/':
res = num1 / num2
else:
print ("Please enter a valid choice")
if res != "":
print("The result is ", res)
Explanation
- When this program will run, the user will see a message saying “Please enter the first number:”. When the user enters a number and press enter, the number will be stored in the variable num1.
- After pressing enter, the user will see another message saying “Please enter the second number:”. The second number entered by the user will be stored in the variable num2.
- After this a third message will appear saying “Please enter choice for operation. + is for addition, – is for subtraction, * is for multiplication and / is for division: “. When the user enters a choice and press enter, the preferred choice will be stored in the variable choice.
- After this, we have declared a variable named as res and initialized it with an empty string. This variable will store the calculated output at the end.
- We have made an if…elif…else tree to check and calculate the output depending upon the choice. For example if the choice is +, the calculation will be the sum of the two numbers.
- In the else part, we are not calculating anything but printing a message = “Please enter a valid choice”. This part will be executed if the user has not entered a choice out of +,-,*, and /.
- Lastly, we are checking the value of res variable. If its value is not an empty string(i.e., something else has been stored in it as the output), we display a message for the output.
Making this program by a user defined-function
We have made a user-defined function that will take three arguments, two numbers and the choice of operation(+, – etc). This function when called, will either return the output or the invalid choice message.
num1 = int(input("Please enter the first number: "))
num2 = int(input("Please enter the second number: "))
choice = input("Please enter choice for operation. + is for addition, - is for subtraction, * is for multiplication and / is for division ")
res = "";
def calculate(num1, num2, choice):
invalidChoice = False
Output = ""
if choice == '+':
res = a + b
elif choice == '-':
res = a - b
elif choice == '*':
res = a * b
elif choice == '/':
res = a / b
else:
invalidChoice = True
if invalidChoice == True:
Output = "Please enter a valid choice for operation"
else:
Output = "The result is {}" . format(res)
return Output
out = calculate(num1, num2, choice)
print(out)
This is a simple calculator program in Python, that only works in the command line. However, we can also make an interactive calculator(which would have a Graphical User Interface) using Tkinter. Since Tkinter is an advanced topic in Python, we will do it in a separate program.