-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser_validation.py
32 lines (28 loc) · 1.31 KB
/
user_validation.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
# import modules
from calculator import Calculator
# Define class for user interface
class UserValidation():
# A method that validates user interaction with the UI
def validate_user(self, num1, num2, operation):
self.calc = Calculator()
if (num1 !='' and num2 !='') and operation != 'Select Operation':
try:
num1 = float(num1)
num2 = float(num2)
# A method that performs calculations based on the selected operation
if operation == 'Addition':
return self.calc.add(num1, num2)
if operation == 'Subtraction':
return self.calc.subtract(num1, num2)
if operation == 'Multiplication':
return self.calc.multiply(num1, num2)
if operation == 'Division':
return self.calc.divide(num1, num2)
# Create exception to handle value error
except ValueError:
raise ValueError(f"\nCalculator could not recognized \n '{num1}' or '{num2}'")
else:
if num1 =='' or num2 =='':
raise ValueError('\nIt looks like you did not fill all the required values.')
else:
raise ValueError('\nHave you chosen a math operation?')