Scientific Computing with Python Projects - Arithmetic Formatter

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:

be careful with your error messages. They need to match the ones in the exercise exactly.

Also please copy the errors you got that you didn’t understand so we can explain them to you.

boilerplate-arithmetic-formatter - Python Repl - Replit .
see for your self

____________________ test_template[test_two_problems_with_solutions] _____________________

arguments = [['3 + 855', '988 + 40'], True]
expected_output = '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.'

>   ???
E   AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.
E   assert "Error: Operator must be '+' or '-'." == '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
E     + Error: Operator must be '+' or '-'.
E     -     3      988
E     - + 855    +  40
E     - -----    -----
E     -   858     1028

/home/runner/boilerplate-arithmetic-formatter/test_module.py:77: AssertionError

This error says that you failed this test:
arithmetic_arranger([[‘3 + 855’, ‘988 + 40’], True]);

It looks like you are not returning the expected arranged values and results.
(all those lines that have the E and minus sign are missing lines)

It seems you are returning an error message instead? “Error: Operator must be …”

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.