Added additional error-checking
This commit is contained in:
		@@ -1,33 +1,46 @@
 | 
				
			|||||||
import re
 | 
					import re
 | 
				
			||||||
from parse import *
 | 
					from parse import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Regular expression that checks for valid characters in an expression
 | 
					# List of valid operators
 | 
				
			||||||
valid_chars = '[0-9.\(\)+-\/*]'
 | 
					opers = ['+', '-', '*', '/']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def raise_error(error_code):
 | 
					# Regular expression that checks for valid characters in an expression
 | 
				
			||||||
 | 
					valid_chars = '[ 0-9.\(\)+-\/*]'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def print_error(error_code):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	# List of error codes:
 | 
						# List of error codes:
 | 
				
			||||||
	# 0 - Invalid characters found in expression
 | 
						# 1 - Invalid characters found in expression
 | 
				
			||||||
	# 1 - Unclosed parantheses
 | 
						# 2 - Unclosed parantheses
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	match error_code:
 | 
						match error_code:
 | 
				
			||||||
		case 0:
 | 
					 | 
				
			||||||
			print("You have invalid characters in your expression.")
 | 
					 | 
				
			||||||
		case 1:
 | 
							case 1:
 | 
				
			||||||
 | 
								print("You have invalid characters in your expression.")
 | 
				
			||||||
 | 
							case 2:
 | 
				
			||||||
			print("You have an unclosed parantheses in your expression.")
 | 
								print("You have an unclosed parantheses in your expression.")
 | 
				
			||||||
	
 | 
							case 3:
 | 
				
			||||||
	main() # Re-enter main loop
 | 
								print("You have two operators next to each other.")
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
def check_errors(expr):
 | 
					def check_errors(expr):
 | 
				
			||||||
	# 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 val in expr:
 | 
				
			||||||
		num_open_pars += 1 if val == '('
 | 
							if not re.match(valid_chars, val):
 | 
				
			||||||
		num_close_pars += 1 if val == ')'
 | 
								return 1
 | 
				
			||||||
		if num_open_pars != num_close_pars:
 | 
					 | 
				
			||||||
			raise_error(2) 
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							num_open_pars = num_open_pars+1 if val == '(' else num_open_pars 
 | 
				
			||||||
 | 
							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 num_open_pars != num_close_pars:
 | 
				
			||||||
 | 
							return 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def evaluate(subexpr): # Evaluate a tokenized expression, that contains no parantheses
 | 
					def evaluate(subexpr): # Evaluate a tokenized expression, that contains no parantheses
 | 
				
			||||||
@@ -84,7 +97,10 @@ def find_inner(subexpr):
 | 
				
			|||||||
def main():
 | 
					def main():
 | 
				
			||||||
	while True:
 | 
						while True:
 | 
				
			||||||
		expr = input()
 | 
							expr = input()
 | 
				
			||||||
		check_for_errors(expr)
 | 
							errno = check_errors(expr)
 | 
				
			||||||
 | 
							if errno != 0:
 | 
				
			||||||
 | 
								print_error(errno)
 | 
				
			||||||
 | 
								continue # If an error was generated, print an error message and continue on to the next iteration of the loop
 | 
				
			||||||
		expr = find_inner(expr)
 | 
							expr = find_inner(expr)
 | 
				
			||||||
		print(expr)
 | 
							print(expr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user