Skip to content

Commit 590d4ee

Browse files
Merge pull request #282 from svadhishenoy95/svadhishenoy95-patch-3
added
2 parents a9d5099 + 8f4bc1c commit 590d4ee

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

calculator.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Program make a simple calculator
2+
3+
# This function adds two numbers
4+
def add(x, y):
5+
return x + y
6+
7+
# This function subtracts two numbers
8+
def subtract(x, y):
9+
return x - y
10+
11+
# This function multiplies two numbers
12+
def multiply(x, y):
13+
return x * y
14+
15+
# This function divides two numbers
16+
def divide(x, y):
17+
return x / y
18+
19+
print("Select operation")
20+
print("1.Add")
21+
print("2.Subtract")
22+
print("3.Multiply")
23+
print("4.Divide")
24+
25+
while True:
26+
# take input from the user
27+
choice = input("Enter choice(1/2/3/4): ")
28+
29+
# check if choice is one of the four options
30+
if choice in ('1', '2', '3', '4'):
31+
num1 = float(input("Enter first number: "))
32+
num2 = float(input("Enter second number: "))
33+
34+
if choice == '1':
35+
print(num1, "+", num2, "=", add(num1, num2))
36+
37+
elif choice == '2':
38+
print(num1, "-", num2, "=", subtract(num1, num2))
39+
40+
elif choice == '3':
41+
print(num1, "*", num2, "=", multiply(num1, num2))
42+
elif choice == '4':
43+
print(num1, "/", num2, "=", divide(num1, num2))
44+
45+
# check if user wants another calculation
46+
# break the while loop if answer is no
47+
next_calculation = input("Let's do next calculation? (yes/no): ")
48+
if next_calculation == "no":
49+
break
50+
else:
51+
print("Invalid Input")

0 commit comments

Comments
 (0)