Tell us what’s happening:
I finished my code, all the conditions were met, but the tests were not validated, I did not understand why. I checked character by character using the repr() function. Everything is ok but it does not work. Please help me !
Your code so far
import re
def digit_finder(char):
return bool(re.search(r'[a-zA-Z]', char))
def lenght_finder(problem):
operands = problem.split()
if len(operands[0]) > 4 or len(operands[2]) > 4:
return True
def longest_operand(operand_1, operand_2):
if len(operand_1)>=len(operand_2):
return len(operand_1)
else:
return len(operand_2)
def arithmetic_arranger(problems, show_answers=False):
problems_string = ' '.join(problems)
place = 0
place_2 = 3
place_3 = 5
time = 1
solution = []
if len(problems) > 5:
return 'Error: Too many problems.'
elif '*' in problems_string or '/' in problems_string:
return "Error: Operator must be '+' or '-'."
elif digit_finder(problems_string) == True:
return "Error: Numbers must only contain digits."
for problem in problems:
if lenght_finder(problem) == True:
return 'Error: Numbers cannot be more than four digits.'
else:
operands = problem.split()
solution.insert(place, ' '*(len(operands[2])-len(operands[0]))+2*' ')
if time == len(problems):
solution.insert(place+1, operands[0]+'\n')
solution.insert(place_2-1, operands[1]+' ')
solution.insert(place_2, ' '*(len(operands[0])-len(operands[2])))
solution.insert(place_2+1, operands[2]+'\n')
if show_answers == True:
solution.insert(place_3,'-'*2+'-'*longest_operand(operands[0], operands[2])+'\n')
if operands[1] == '-':
if int(operands[0])-int(operands[2]) < 0:
if len(str(int(operands[0])-int(operands[2]))) > longest_operand(operands[0], operands[2]):
solution.append(1*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])-int(operands[2]))))*' '+str(int(operands[0])-int(operands[2])))
else:
solution.append(2*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])-int(operands[2]))))*' '+str(int(operands[0])-int(operands[2])))
else:
solution.append(2*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])-int(operands[2]))))*' '+str(int(operands[0])-int(operands[2])))
else:
if len(str(int(operands[0])+int(operands[2]))) > longest_operand(operands[0], operands[2]):
solution.append(1*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])+int(operands[2]))))*' '+str(int(operands[0])+int(operands[2])) + 4*' ')
else:
solution.append(2*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])+int(operands[2]))))*' '+str(int(operands[0])+int(operands[2])) + 4*' ')
else:
solution.append('-'*2+'-'*longest_operand(operands[0], operands[2]))
return ''.join(solution)
else:
solution.insert(place+1, operands[0]+4*' ')
solution.insert(place_2-1, operands[1]+' ')
solution.insert(place_2, ' '*(len(operands[0])-len(operands[2])))
solution.insert(place_2+1, operands[2]+4*' ')
if show_answers == True:
solution.insert(place_3,'-'*2+'-'*longest_operand(operands[0], operands[2])+4*' ')
if operands[1] == '-':
if int(operands[0])-int(operands[2]) < 0:
if len(str(int(operands[0])-int(operands[2]))) > longest_operand(operands[0], operands[2]):
solution.append(1*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])-int(operands[2]))))*' '+str(int(operands[0])-int(operands[2]))+4*' ')
else:
solution.append(2*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])-int(operands[2]))))*' '+str(int(operands[0])-int(operands[2]))+4*' ')
else:
solution.append(2*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])-int(operands[2]))))*' '+str(int(operands[0])-int(operands[2]))+4*' ')
else:
if len(str(int(operands[0])+int(operands[2]))) > longest_operand(operands[0], operands[2]):
solution.append(1*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])+int(operands[2]))))*' '+str(int(operands[0])+int(operands[2])) + 4*' ')
else:
solution.append(2*' '+(longest_operand(operands[0], operands[2])-len(str(int(operands[0])+int(operands[2]))))*' '+str(int(operands[0])+int(operands[2])) + 4*' ')
else:
solution.append('-'*2+'-'*longest_operand(operands[0], operands[2])+4*' ')
place += 2
place_2 += 5
place_3 += 6
time += 1
print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "988 + 40", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project