howt to make caluclator in python

Solutions on MaxInterview for howt to make caluclator in python by the best coders in the world

showing results for - "howt to make caluclator in python"
Maude
23 Jan 2017
1# Program make a simple calculator
2
3# This function adds two numbers
4def add(x, y):
5    return x + y
6
7# This function subtracts two numbers
8def subtract(x, y):
9    return x - y
10
11# This function multiplies two numbers
12def multiply(x, y):
13    return x * y
14
15# This function divides two numbers
16def divide(x, y):
17    return x / y
18
19
20print("Select operation.")
21print("1.Add")
22print("2.Subtract")
23print("3.Multiply")
24print("4.Divide")
25
26while True:
27    # Take input from the user
28    choice = input("Enter choice(1/2/3/4): ")
29
30    # Check if choice is one of the four options
31    if choice in ('1', '2', '3', '4'):
32        num1 = float(input("Enter first number: "))
33        num2 = float(input("Enter second number: "))
34
35        if choice == '1':
36            print(num1, "+", num2, "=", add(num1, num2))
37
38        elif choice == '2':
39            print(num1, "-", num2, "=", subtract(num1, num2))
40
41        elif choice == '3':
42            print(num1, "*", num2, "=", multiply(num1, num2))
43
44        elif choice == '4':
45            print(num1, "/", num2, "=", divide(num1, num2))
46        break
47    else:
48        print("Invalid Input")