Let us take a look at an approach to solving this problem statement.
def add_num(x, y): return x + y def subtract_num(x, y): return x - y def multiply_num(x, y): return x * y def divide_num(x, y): return x / y
Explanation:
Here in this part, we have defined various functions to handle the arithmetic operations. Like addition, subtraction, multiplication, and division. In the addition function, we simply passed the input of the user and used the + operator to perform addition. The same goes for subtraction, multiplication, and division wherein we used the -, *, and / operators respectively.
print("Select the operation you want to perform") print("1.Add") print("2.Subtract") print("3.Multiply") print("4.Divide")
Explanation:
We have assigned 1 to perform addition, 2 to perform Subtraction, 3 to perform Multiplication and 4 to perform Division.
while True: choice = input("Enter choice(1/2/3/4): ") if choice in ('1', '2', '3', '4'): try: num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) except ValueError: print("Invalid input. Please enter a number.") continue if choice == '1': print("The addition of : ", num1, "+", num2, "=", add_num(num1, num2)) elif choice == '2': print("The subtraction of : ",num1, "-", num2, "=", subtract_num(num1, num2)) elif choice == '3': print("The multiplication of : ",num1, "*", num2, "=", multiply_num(num1, num2)) elif choice == '4': print("The division of : ",num1, "/", num2, "=", divide_num(num1, num2)) another_calc = input("Let's do next calculation? (yes/no): ") if another_calc == "no": break else: print("Invalid Input. Please enter valid input")
Explanation:
Now we used the while loop to provide the user with an option to select the operations that they want to perform. We took the choice variable to take the input from the user and then used the try and except block to handle the error if the user enters some other number apart from 1 to 4. Now if the user enters 1 then the addition function comes into the picture and the same goes for 2,3 and 4. After each operation is performed the program asks the user if he/she wants to go on with another operation or not.
Output:
Select the operation you want to perform
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 1
Enter first number: 32
Enter second number: 34
The addition of : 32.0 + 34.0 = 66.0
Let's do next calculation? (yes/no): yes
Enter choice(1/2/3/4): 2
Enter first number: 32
Enter second number: 54
The subtraction of : 32.0 - 54.0 = -22.0
Let's do next calculation? (yes/no): yes
Enter choice(1/2/3/4): 3
Enter first number: 34
Enter second number: 2
The multiplication of : 34.0 * 2.0 = 68.0
Let's do next calculation? (yes/no): yes
Enter choice(1/2/3/4): 4
Enter first number: 32
Enter second number: 8
The division of : 32.0 / 8.0 = 4.0
Let's do next calculation? (yes/no): no
Method 2: Using the switcher
def addition_num(num1, num2): num1 += num2 return num1 def subtraction_num(num1, num2): num1 -= num2 return num1 def mul_num(num1, num2): num1 *= num2 return num1 def division_num(num1, num2): num1 /= num2 return num1 def default(num1, num2): return "Incorrect day" switcher = { 1: addition_num, 2: subtraction_num, 3: mul_num, 4: division_num, } def switch(operation, num1, num2): return switcher.get(operation, default)(num1, num2) print('''You can perform operation 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Module ''') # Take input from user choice = int(input("Select operation from 1,2,3,4 : ")) num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) print (switch(choice, num1, num2))
Explanation:
Here we used a switcher that simply keeps track of the functions that need to be called.
Here is the end of this article on developing a simple calculator. We have neatly defined functions and learned to handle unexpected input from the user. Try adding your own function and make it a bit more advanced.