Added additional error checking, for number formatting and consecutive operators

master
Aadhavan Srinivasan 1 year ago
parent 910b6357a2
commit 88902d9066

@ -12,6 +12,8 @@ def print_error(error_code):
# List of error codes:
# 1 - Invalid characters found in expression
# 2 - Unclosed parantheses
# 3 - Two operators next to each other
# 4 - Wrong number formatting (multiple periods)
match error_code:
case 1:
@ -20,12 +22,16 @@ def print_error(error_code):
print("You have an unclosed parantheses in your expression.")
case 3:
print("You have two operators next to each other.")
case 4:
print("One of your values is improperly formatted.")
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
num_open_pars = 0
num_close_pars = 0
for val in expr:
for index,val in enumerate(expr_small):
if not re.match(valid_chars, val):
return 1
@ -33,13 +39,24 @@ def check_errors(expr):
num_close_pars = num_close_pars+1 if val == ')' else num_close_pars
if val in opers:
#Check if the next element is an operator. If it is, throw an error
continue
if expr_small[index + 1] in opers: # Two consecutive operators
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:
return 2
expr = parse(expr)
for val in expr:
if val.count('.') > 1:
return 4
return 0

Loading…
Cancel
Save