-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculator.py
50 lines (34 loc) · 1.33 KB
/
calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#Write a program to make a simple calculator. The program should perform four basic operations (1-addition, 2-subtraction, 3-multiplication, and 4-division)
#depending on the user’s choice.
#The program will take two values from the user and the type of operation. The result will be displayed based on the operation chosen by the user.
#The program will be repeated until user requests to stop.
def addition(a, b):
return a + b
def subtraction(a, b):
return a - b
def multiplication(a, b):
return a * b
def division(a, b):
return a / b
print("Select operation.")
print("1.Addition")
print("2.Subtraction")
print("3.Multiplication")
print("4.Division")
print(' ')
while True:
operation = input("Choose operation(1/2/3/4): ")
if operation in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if operation == '1':
print(num1, "+", num2, "=", addition(num1, num2))
elif operation == '2':
print(num1, "-", num2, "=", subtraction(num1, num2))
elif operation == '3':
print(num1, "*", num2, "=", multiplication(num1, num2))
elif operation == '4':
print(num1, "/", num2, "=", division(num1, num2))
break
else:
print("You entered the invalid input")