-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculatorApp.py
90 lines (68 loc) · 2.11 KB
/
calculatorApp.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
81
82
83
84
85
86
87
88
89
90
# Simple calculator
def check_user_input(input):
if input == "":
print("Input can't be empty")
raise ValueError("Input can't be empty")
try:
# Convert it into integer
val = int(input)
return val
except ValueError:
try:
# Convert it into float
val = float(input)
return val
except ValueError:
print(input + " input is not a number!")
raise ValueError(input + " input is not a number!")
def add(x, y):
result = x + y
return result
# This function subtracts two numbers
def subtract(x, y):
result = x - y
return result
# This function multiplies two numbers
def multiply(x, y):
result = x * y
return result
# This function divides two numbers
def divide(x, y):
if y == 0:
print("You can't divide by zero!")
raise ZeroDivisionError("You can't divide by zero!")
elif x == 0:
return 0
else:
result = x / y
return result
def calculate(choice, num1, num2):
# check if choice is one of the four options
if not num1 or not num2:
print("inputs can not be null")
raise ValueError("inputs can not be null")
num1 = check_user_input(num1)
num2 = check_user_input(num2)
result = ""
if choice in ('1', '2', '3', '4'):
if choice == '1':
result = add(num1, num2)
elif choice == '2':
result = subtract(num1, num2)
elif choice == '3':
result = num1, "*", num2, "=", multiply(num1, num2)
elif choice == '4':
if num2 == 0:
print("You can't divide by zero!")
raise ZeroDivisionError("You can't divide by zero!")
result = num1, "/", num2, "=", divide(num1, num2)
else:
raise Exception("Invalid choice!")
return result
# this method return true if the user type no and return false if user type yes.
def isExit(next_calculation):
if next_calculation == "no":
return True
elif next_calculation == "yes":
return False
raise ValueError("Invalid choice")