Tell us what’s happening:
Not sure where to even begin. I thought I’d at least get output for x and y but nothing. I could use tips for this project as well as advice on troubleshooting outside this website (troubleshooting here is honestly pretty terrible).
Your code so far
def arithmetic_arranger(problems, disp=False):
arranged_problems = [] # List for returning formatted problems
x = [str.split()[0] for str in problems] # Top row
y = [str.split()[2] for str in problems] # Bottom row
operations = [str.split()[1] for str in problems] # Valid operators
solutions = [] # List for calculated solutions
print(x)
print(y)
# Check for errors
if len(problems) > 5:
raise ValueError("Error: Too many problems.")
elif not all(op in {'+', '-'} for op in operations):
raise ValueError("Error: Operator must be '+' or '-'.")
elif not all(map(lambda a: a.isdigit(), x)) or not all(map(lambda a: a.isdigit(), y)):
raise ValueError("Numbers must only contain digits.")
elif not all(map(lambda a: len(a) < 5, x)) and not all(map(lambda a: len(a) < 5, y)):
raise ValueError("Numbers cannot be more than four digits.")
else:
for i in range(0, len(problems)):
# Calculate width for formatting
width = max(len(x[i]), len(y[i])) + 2
# Calculate solutions
if disp:
if operations[i] == '+':
solution = str(int(x[i]) + int(y[i]))
else:
solution = str(int(x[i]) - int(y[i]))
solutions.append(solution)
top_row.append(f"{x[i]:>{width}}" + ' ')
bottom_row.append(f"{operations[i]:<{width}} + {y[i]:>{width}}" + ' ')
dash_line.append('-' * width + ' ')
if disp:
sol_row.append(f"{solutions[i]:>{width}}" + ' ')
if disp:
arranged_problems = '\n'.join(top_row, bottom_row, dash_line, sol_row)
else:
arranged_problems = '\n'.join(top_row, bottom_row, dash_line)
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project