Hello! sorry for the repetitive subject and the bad English! but I couldn’t find help elsewhere , so I was done with the Arithmetic Formatter project, my code works well outside of the Freecodecamp console and even inside of it , yet it keep showing me error.
this is my code :
#!python3
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return 'Error: Too many problems.'
cond_1 = map(lambda x : x.split()[1], problems)
if set(cond_1) != {'+', '-'}:
return "Error: Operator must be '+' or '-'."
cond_2 = map(lambda x : x.split(), problems)
for item in cond_2:
if(item[0].isdigit() and item[2].isdigit()) is False:
return 'Error: Numbers must only contain digits.'
if len(item[0]) > 4 or len(item[2]) > 4:
return 'Error: Numbers cannot be more than four digits.'
first = ''
second = ''
third = ''
fourth = ''
for problem in problems:
width = max(len(problem.split()[0]), len(problem.split()[2]))
first_Num = problem.split()[0].rjust(width+2)
second_Num = f'{problem.split()[1]} {problem.split()[2].rjust(width)}'
_dashes = '-' * (width +2)
if problem.split()[1] == '+':
result = str(int(problem.split()[0]) + int(problem.split()[2])).rjust(width+2)
if problem.split()[1] == '-':
result = str(int(problem.split()[0]) - int(problem.split()[2])).rjust(width+2)
if problems != problems[-1]:
first += first_Num + ' '
second += second_Num + ' '
third += _dashes + ' '
fourth += result + ' '
else:
first += first_Num
second += second_Num
third += _dashes
fourth += result
if show_answers :
res = first + '\n' + second + '\n' + third + '\n' + fourth
else:
res = first + '\n' + second + '\n' + third
return res
print(arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49", "80 + 30"]))
I still can’t see the problem since this is the output I get on the Freecodecamp console as well, if I try to tweak the spacing and stuff, it’s no longer aligned, thanks anyway I’ll keep trying.