Tell us what’s happening:
My output is exactly what is required for each of the 10 test cases but it is still failing every single one of them. I am attaching the error messages with my output. I am new to coding so please let me know if the error means something i can’t see.
Your code so far
class MyException(Exception):
def __init__(self, message):
self.message= message
import re
import sys
def arithmetic_arranger1(expression, output="False"):
'''This function takes at most 5 addition or substraction problems
supplied in the expression as a list. Each problem must be supplied
as a string literal. For example, "23 + 456"
length of each digit shouldn't be bigger than 4.
expression: list of problems supplied as a list of strings
output: if True, the result is supplied to the user
'''
#check if total number of problems supplied is within limit
if len(expression)>5:
raise MyException('Error: Too many problems')
sys.exit()
numerator=[]
denominator=[]
operator=[]
result=[]
for i in range(len(expression)):
#all edge cases handled.
prob = re.findall('[^\+\-\s]+|\+|\-',expression[i])
numerator.append(prob[0])
denominator.append(prob[2])
operator.append(prob[1])
for i in range(len(expression)):
#lets handle the exceptions first
#conversion error
try:
int(numerator[i])
int(denominator[i])
except ValueError:
print('Error: Numbers must only contain digits')
return
#operator error
if operator[i] not in ('+','-'):
raise MyException("Error: Operator must be '+' or '-' ")
sys.exit()
if len(numerator[i])>4 or len(denominator[i])>4:
raise MyException("Error: Numbers cannot be more than four digits")
for i in range(len(expression)): #print first row
print(' ', end='') #two spaces for operator sign in second row
if len(numerator[i])<len(denominator[i]):
space = len(denominator[i])-len(numerator[i])
print(' '*space, end='')
print(int(numerator[i]), end='')
print(' '*4, end='')
print("")
for i in range(len(expression)): #print second row with operators
print(operator[i]+' ', end='')
if len(numerator[i])>len(denominator[i]):
space = len(numerator[i])-len(denominator[i])
print(' '*space, end='')
print(int(denominator[i]), end='')
print(' '*4, end='')
print("")
for i in range(len(expression)): #print third row of dashes
a=max(len(numerator[i]),len(denominator[i]))
print('-'*(2+a)+' '*4, end='')
print("")
if output==True:
for i in range(len(expression)): #print fourth row
#Exception has already been handled so we only get + or -
if operator[i]=='+':
result.append(int(numerator[i])+int(denominator[i]))
else:
result.append(int(numerator[i])-int(denominator[i]))
a=max(len(numerator[i]),len(denominator[i]))+2 #max length
space= a - len(str(result[i]))
print(' '*space+str(result[i])+' '*4, end='')
def arithmetic_arranger(expression, option=False):
try:
arithmetic_arranger1(expression, option)
except MyException as e:
print(e)
except:
print('Something went wrong')
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: