Tell us what’s happening:
Hi,
I’m wondering to know what’s wrong in my code for the 1st projet “Build Arithmetic formatter” of the curriculum “Scientific Computing with Python”.
All tests failed but this check:
print(arithmetic_arranger([“32 - 698”, “1 - 3801”, “45 + 43”, “123 + 49”, “988 + 40”], True)==" 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028") → True
Thanks for helps
Your code so far
def arithmetic_arranger(problems, show_answers=False):
#print("Hello world!")
checkProblemsLength(problems)
up = ''
middle = ''
dashes = ''
result = ''
for prob in problems:
tab = prob.split(' ')
checkOperand(tab[0])
checkOperand(tab[2])
checkOperandLen(tab[0])
checkOperandLen(tab[2])
checkOperator(tab[1])
output = displayOperation(tab[0], tab[1], tab[2])
up += output["operand1"] + ' '
middle += output["operand2"] + ' '
dashes += output["dashes"] + ' '
result += output["result"] + ' '
if show_answers:
out = up.rstrip()+"\\n"+middle.rstrip()+"\\n"+dashes.rstrip()+"\\n"+result.rstrip()
else:
out = up.rstrip()+"\\n"+middle.rstrip()+"\\n"+dashes.rstrip()
return out
# Check values
def checkProblemsLength(probs):
if len(probs) > 5:
raise ValueError('Error: Too many problems.')
def checkOperandLen(operand):
if len(operand) > 4:
raise ValueError('Error: Numbers cannot be more than four digits.')
def checkOperand(operand):
if not operand.isdigit():
raise ValueError('Error: Numbers must only contain digits.')
def checkOperator(operator):
if not operator == '+' and not operator == '-':
raise ValueError("Error: Operator must be '+' or '-'.")
def displayOperation(operand1, sign, operand2):
output = {}
maxLength = max(len(operand1), len(operand2))
dashes = ''.join(['-' for x in range(0, maxLength + 2)])
line1 = ''.join([' ' for x in range(0, maxLength + 2 - len(operand1))]) + operand1
line2 = sign + ''.join([' ' for x in range(0, maxLength + 1 - len(operand2))]) + operand2
output["operand1"] = line1
output["operand2"] = line2
output["dashes"] = dashes
if sign == '+':
result = int(operand1) + int(operand2)
else:
result = int(operand1) - int(operand2)
line3 = ''.join([' ' for x in range(0, maxLength + 2 - len(str(result)))]) + str(result)
output["result"] = line3
return output
print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)==" 32 1 45 123 988\\n- 698 - 3801 + 43 + 49 + 40\\n----- ------ ---- ----- -----\\n -666 -3800 88 172 1028")
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project