-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
80 lines (51 loc) · 1.63 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Simple calculator
def addition(x, y):
sum = float(x) + float(y)
print(sum)
def subtraction(x, y):
operation = float(x) - float(y)
print(operation)
def multiplication(x, y):
multiply = float(x) * float(y)
print(multiply)
def division(x, y):
divide = float(x) / float(y)
print(divide)
def exponentiation(x, y):
exp = float(x) ** float(y)
print(exp)
def modulo(x, y):
modul = float(x) % float(y)
print(modul)
def get_numbers():
x, y = (input("Enter two numbers separated by a space: ").split())
while x.isalpha():
print("Incorrect format, enter a number")
x = input("Enter the first number: ")
while y.isalpha():
print("Incorrect format, enter a number")
y = input("Enter the second number: ")
return x, y
def calculator():
x, y = get_numbers()
print(f"x= {x}")
print(f"y= {y}")
while True:
arithmetic = input("What math operation do you want to perform?: ").lower()
if arithmetic == 'add':
addition(x, y)
elif arithmetic == 'subtract':
subtraction(x, y)
elif arithmetic == 'multiply':
multiplication(x, y)
elif arithmetic == 'divide':
division(x, y)
elif arithmetic == 'exp':
exponentiation(x, y)
elif arithmetic == 'modulo':
modulo(x, y)
elif arithmetic == 'quit':
quit()
else:
print("Enter the correct math operation: (add/subtract/multiply/divide/exp/modulo/quit)")
calculator()