Tell us what’s happening:
I’m failing the test cases with the second argument = True, but when I run the code locally the output looks right. I would be super duper appreciative if anyone can spot my error…
I’m pasting my entire ‘arithmetic_arranger.py’ file.
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.’
and
fail_message = ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with five arithmetic problems and a second argument of True.’
Your code so far
def arithmetic_arranger(problems, solve=False):
class classProblem:
def __init__(self, operand1: int, operand2: int, operator: str,
solve: bool):
self.solve = solve
self.operand1 = operand1
self.operand2 = operand2
self.operator = operator
if operator == '+':
self.solution = operand1 + operand2
elif operator == '-':
self.solution = operand1 - operand2
if not solve:
self.maxLength = max(len(str(operand1)), len(str(operand2))) + 2
elif solve:
self.maxLength = len(str(self.solution)) + 2
if len(problems) > 5:
return “Error: Too many problems.”
objectProblems: list[classProblem] =
for problem in problems:
listProblem = problem.split()
if len(listProblem[0]) > 4 or len(listProblem[2]) > 4:
return “Error: Numbers cannot be more than four digits.”
if not listProblem[0].isnumeric() or not listProblem[2].isnumeric():
return “Error: Numbers must only contain digits.”
operand1 = int(listProblem[0])
operand2 = int(listProblem[2])
operator = listProblem[1]
if operator != ‘+’ and operator != ‘-’:
return “Error: Operator must be ‘+’ or ‘-’.”
objectProblem = classProblem(operand1, operand2, operator, solve)
objectProblems.append(objectProblem)
line1 = " ".join(
str(problem.operand1).rjust(problem.maxLength)
for problem in objectProblems)
line2 = " “.join(
f”{problem.operator} {str(problem.operand2).rjust(problem.maxLength - 2)}"
for problem in objectProblems)
line3 = " “.join(”-" * problem.maxLength for problem in objectProblems)
if solve:
line4 = " “.join(
str(problem.solution).rjust(problem.maxLength)
for problem in objectProblems)
arranged_problems = f”{line1}\n{line2}\n{line3}\n{line4}"
elif not solve:
arranged_problems = f"{line1}\n{line2}\n{line3}"
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36
Challenge Information:
Scientific Computing with Python Projects - Arithmetic Formatter
