Hi, in this article let's learn how to create a Python simple calculator using if else statements. I will only be using the if else statement and then perform automatic operations on the given number input to the Python program.
I will also be using the eval() function to evaluate the Arithmetic operations.
Write A Python Script To Create Simple Calculator Using If-Else-If Statement
Take a function name that get_choice(), ask a user to enter a number, this number is a user choice for addition, subtraction multiplication and division, and the user can also opt for creating the program by selecting the choice number 5.
def get_choice():
return int(input("\n1: Add \n2: Subtract \n3: Multiply \n4: Divide \n5: Quit \nEnter Choice : "))
Let take another function named as a evaluate(), pass the operator to this function, the operator is a string type +, -, * or /. use the input function call to Tu of the user to enter two numbers, storage into two variables X and Y.
Using the if statement checks if Y is equal to zero and operator is division operator /. If so , printing an error message divided by zero is not possible. using the else block evaluates the Arithmetic operation by passing X and Y values as an input to the eval() function and then printing the result.
def evaluate(operator):
x = input("\nEnter First Operand : ")
y = input("Enter Second Operand : ")
if float(y) == 0 and operator == "/":
print("Divide by zero not possible")
else:
result = eval(x + operator + y)
print(f"{x} {operator} {y} is : {result}")
In the main program use a while loop to iterate an infinite loop and ask a user to enter a choice by calling the get_choice() function call.
Using the if else block
- Check if choice equals to 1 call the evaluate function with the operator +
- Check if the choice is equals to 2 then called the evaluate function with the - operator
- Check if the choice is equals to 3 then called the evaluate function with the multiplication operator
- If the choice is equals to 4 then call the evaluate function with division operator
- If the choice is 5 then print a message to say goodbye and break the while loop to exit the program
while True:
choice = get_choice()
if choice == 1: evaluate("+")
elif choice == 2: evaluate("-")
elif choice == 3: evaluate("*")
elif choice == 4: evaluate("/")
elif choice == 5:
print("Exiting Calculator, Bye !")
break
Complete python code
=============================
def get_choice():
return int(input("\n1: Add \n2: Subtract \n3: Multiply \n4: Divide \n5: Quit \nEnter Choice : "))
def evaluate(operator):
x = input("\nEnter First Operand : ")
y = input("Enter Second Operand : ")
if float(y) == 0 and operator == "/":
print("Divide by zero not possible")
else:
result = eval(x + operator + y)
print(f"{x} {operator} {y} is : {result}")
while True:
choice = get_choice()
if choice == 1: evaluate("+")
elif choice == 2: evaluate("-")
elif choice == 3: evaluate("*")
elif choice == 4: evaluate("/")
elif choice == 5:
print("Exiting Calculator, Bye !")
break
Output
==================
1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 4
Enter First Operand : 34
Enter Second Operand : 0
Divide by zero not possible
1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 4
Enter First Operand : 25
Enter Second Operand : 5
25 / 5 is : 5.0
1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 1
Enter First Operand : 45
Enter Second Operand : 45
45 + 45 is : 90
1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 2
Enter First Operand : 90
Enter Second Operand : 60
90 - 60 is : 30
1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 3
Enter First Operand : 2
Enter Second Operand : 3
2 * 3 is : 6
1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 5
Exiting Calculator, Bye !
Conclusion
=============
Execute the above Python program by providing a different set of input values and perform the arithmetic operations.
Try to optimize the above Python program as much as possible, comment down below if you have any suggestions to optimize or reduce the number of lines of code.
0 Comments