Tell us what’s happening:
I get errors and i dont know what is wrong with the code
import re
def arithmetic_arranger(problems, solution = False):
if (len(problems) > 5):
return ‘Too many problems!!!’
first = “”
second = “”
lines = “”
sumt = “”
string = “”
for p in problems:
if(re.search(‘[^\s0-9.±]’, p)):
if(re.search(‘[/]’, p) or re.search(‘[*]’, p)):
return ‘Error: operator should be ‘+’ or ‘-’’
return ‘Error: Numbers needs to be only digits.’
firstNum = p.split(’ ‘)[0]
operator = p.split(’ ‘)[1]
secNum = p.split(’ ')[2]
if(len(firstNum) >= 5 or len(secNum) >= 5):
return 'Error: Numbers can`t be mroe than 4 digits!'
sum = ""
if(operator == '+'):
sum = str(int(firstNum) + int(secNum))
elif(operator == '-'):
sum = str(int(firstNum) - int(secNum))
length = max(len(firstNum), len(secNum)) + 2
top = str(firstNum).rjust(length)
bottom = operator + str(secNum).rjust(length - 1)
line = ""
res = str(sum).rjust(length)
for s in range(length):
line += '-'
if p != problems[-1]:
first += top + ' '
second += bottom + ' '
lines += line + ' '
sumt += res + ' '
else:
first += top
second += bottom
lines += line
sumt += res
if solution:
string = first + ‘\n’ + second + ‘\n’ + lines + ‘\n’ + sumt
else:
string = first + ‘\n’ + second + ‘\n’ + lines
return string
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: