I’ve completed the first project, the arithmetic formatter, and tested the code myself. It catches all errors as it should and formatts the answer as specified. However the testing program on replit gives me two failures on the format and the answers.
What should I do?
try to figure out why it is failing, or post your project here so we can see the errors and help you
Here’s my code, the thing is that it prints everything as it should, the test program fails it though
def arithmetic_arranger(problems, solutions=bool):
operand1 = []
operator = []
operand2 = []
results = []
op_length = []
# check the number of problems
if len(problems) > 5:
return 'Error: Too many problems.'
# split the problems into lists of operators and first and second operands
for i in range(len(problems)):
operand1.append(problems[i].split()[0])
operator.append(problems[i].split()[1])
operand2.append(problems[i].split()[2])
# check for invalid operators
for i in operator:
if not i == '+':
if not i == '-':
return "Error: Operator must be '+' or '-'."
# check for invalid operands
for i in operand1:
if not i.isnumeric():
return "Error: Numbers must only contain digits."
for i in operand2:
if not i.isnumeric():
return "Error: Numbers must only contain digits."
# check size of numbers
for i in operand1:
if len(i) > 4:
return "Error: Numbers cannot be more than four digits."
for i in operand2:
if len(i) > 4:
return "Error: Numbers cannot be more than four digits."
# compare length of individual operands
for i in range(len(problems)):
if len(operand1[i]) > len(operand2[i]):
op_length.append(len(operand1[i]))
else:
op_length.append(len(operand2[i]))
# arrange the output
space = ' '
first_line = ''
second_line = ''
third_line = ''
fourth_line = ''
for i in range(len(problems)):
first_line = first_line + space * (2 + op_length[i] - len(operand1[i])) + operand1[i] + space * 4
second_line = second_line + operator[i] + space * (1 + op_length[i] - len(operand2[i])) + operand2[
i] + space * 4
third_line = third_line + '-' * (2 + op_length[i]) + space * 4
arranged_problems = first_line + '\n' + second_line + '\n' + third_line
# calculate the results of the problems
if solutions:
for i in range(len(problems)):
if operator[i] == '+':
results.append(int(operand1[i]) + int(operand2[i]))
else:
results.append(int(operand1[i]) - int(operand2[i]))
for i in range(len(problems)):
fourth_line = fourth_line + space*(2 + op_length[i] - len(str(results[i]))) + str(results[i]) + space*4
arranged_problems = arranged_problems + '\n' + fourth_line
return arranged_problems
can you share the project link or at least the error messages?
so, you have a couple of tests failing
AssertionError: ' [23 chars] 123 \n+ 855 - 2 + 43 + 49 [73 chars] ' != ' [23 chars] 123\n+ 855 - 2 + 43 + 49\n-----[23 chars]----'
- 3 3801 45 123
? ----
+ 3 3801 45 123
- + 855 - 2 + 43 + 49
? ----
+ + 855 - 2 + 43 + 49
- ----- ------ ---- -----
? -----
+ ----- ------ ---- -----
- 858 3799 88 172
the lines starting with -
are your wrong code, the lines starting with +
are instead the expected result
from there you should be able to get why the test is failing
So if I understand things correctly, the problem is the empty spaces after each line of numbers? Which means that my code does what it should but not the way that is expected for the project?
that’s one, but you also have the solutions when it is not requested
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.