Added additional error checking, for number formatting and consecutive operators
This commit is contained in:
@@ -12,6 +12,8 @@ def print_error(error_code):
|
|||||||
# List of error codes:
|
# List of error codes:
|
||||||
# 1 - Invalid characters found in expression
|
# 1 - Invalid characters found in expression
|
||||||
# 2 - Unclosed parantheses
|
# 2 - Unclosed parantheses
|
||||||
|
# 3 - Two operators next to each other
|
||||||
|
# 4 - Wrong number formatting (multiple periods)
|
||||||
|
|
||||||
match error_code:
|
match error_code:
|
||||||
case 1:
|
case 1:
|
||||||
@@ -20,12 +22,16 @@ def print_error(error_code):
|
|||||||
print("You have an unclosed parantheses in your expression.")
|
print("You have an unclosed parantheses in your expression.")
|
||||||
case 3:
|
case 3:
|
||||||
print("You have two operators next to each other.")
|
print("You have two operators next to each other.")
|
||||||
|
case 4:
|
||||||
|
print("One of your values is improperly formatted.")
|
||||||
|
|
||||||
def check_errors(expr):
|
def check_errors(expr):
|
||||||
|
expr_small = expr.replace(" ", "") # Remove spaces from the string, to make it easier to parse
|
||||||
|
|
||||||
# Check if number of opening parantheses is equal to number of closing parantheses
|
# Check if number of opening parantheses is equal to number of closing parantheses
|
||||||
num_open_pars = 0
|
num_open_pars = 0
|
||||||
num_close_pars = 0
|
num_close_pars = 0
|
||||||
for val in expr:
|
for index,val in enumerate(expr_small):
|
||||||
if not re.match(valid_chars, val):
|
if not re.match(valid_chars, val):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@@ -33,13 +39,24 @@ def check_errors(expr):
|
|||||||
num_close_pars = num_close_pars+1 if val == ')' else num_close_pars
|
num_close_pars = num_close_pars+1 if val == ')' else num_close_pars
|
||||||
|
|
||||||
if val in opers:
|
if val in opers:
|
||||||
#Check if the next element is an operator. If it is, throw an error
|
if expr_small[index + 1] in opers: # Two consecutive operators
|
||||||
continue
|
return 3
|
||||||
|
|
||||||
|
if val == '.':
|
||||||
|
if not expr_small[index + 1].isdigit(): # If you have a period, you must have a number after it
|
||||||
|
return 4
|
||||||
|
|
||||||
|
|
||||||
if num_open_pars != num_close_pars:
|
if num_open_pars != num_close_pars:
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
expr = parse(expr)
|
||||||
|
for val in expr:
|
||||||
|
if val.count('.') > 1:
|
||||||
|
return 4
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user