-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
45 lines (37 loc) · 1.23 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
def addition(x, y):
return x + y
def substraction(x, y):
return x - y
def multiplication(x, y):
return x * y
def dividation(x, y):
if y == 0:
return ('Can not divide by zero!')
return x / y
while True:
print('Select operation.')
print('1. Addition')
print('2. Substraction')
print('3. Multiplication')
print('4. Dividation')
print('5. Exit')
userinput = input(": ")
if userinput in ("Addition", "Substraction", "Multiplication", "Dividation", "Exit"):
while True:
try:
x = float(input("Enter first number: "))
y = float(input("Enter second number: "))
break
except ValueError:
print("Invalid input, please enter a number!")
if userinput == "Addition":
print(x, "+", y, "=", addition(x, y))
elif userinput == "Substraction":
print(x, "-", y, "=", substraction(x, y))
elif userinput == "Multiplication":
print(x, "*", y, "=", multiplication(x, y))
elif userinput == "Dividation":
print(x, "/", y, "=", dividation(x, y))
elif userinput == "Exit":
break
print("Thank you for using calculator!")