When I use my code in an IDE, I “pass” the tests but on FCC I do not. I definitely think it’s formatted right but obviously there is something wrong.
please just some hints would be nice I don’t get it at all???
Your code so far
def arithmetic_arranger(problems, show_answers=False):
top_rows = []
bottom_rows = []
symbol_rows = []
answers = []
if len(problems) > 5:
print("Error: Too many problems.")
for problem in problems:
operands = problem.split()
first_operands, operators, second_operands = operands
if operators not in ["+", "-"]:
print("Operator must be '+' or '-'.")
continue
if not (first_operands.strip().isdigit() and second_operands.strip().isdigit()):
print("Numbers must only contain digits.")
if len(first_operands) > 4 or len(second_operands) > 4 :
print("Numbers cannot be more than four digits.")
width = max(len(first_operands), len(second_operands)) + 2
top_rows.append(first_operands.rjust(width))
bottom_rows.append(f"{operators} {second_operands.rjust(width - 2)}")
symbol_rows.append("-" * width)
aligned_top = " ".join(top_rows)
aligned_bottom = " ".join(bottom_rows)
aligned_symbols = " ".join(symbol_rows)
if show_answers:
if operators == "+":
answer = int(first_operands) + int(second_operands)
elif operators == "-":
answer = int(first_operands) - int(second_operands)
answers.append(str(answer).rjust(width))
#largest_operand = max(len(str(first_operands)), len(str(second_operands)))
aligned_top = " ".join(top_rows)
aligned_bottom = " ".join(bottom_rows)
aligned_symbols = " ".join(symbol_rows)
aligned_answers = " ".join(answers)
if show_answers:
print(f"{aligned_top}\n{aligned_bottom}\n{aligned_symbols}\n{aligned_answers}")
else:
print(f"{aligned_top}\n{aligned_bottom}\n{aligned_symbols}")
return problems
arithmetic_arranger(["3801 - 2", "123 + 49"])
Challenge: Step 40
Link to the challenge: