Tell us what’s happening:
I don’t understand why it doesn’t work. My code works, but when I run the tests, it gives me an ‘X’ saying it doesn’t. Unless I’m supposed to make it print ex:’ 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028’, I don’ understand the issue.
Your code so far
def arithmetic_arranger(problems, argument2 = False):
equations = problems
problems = [e.replace(" ", "") for e in equations]
for problem in problems:
if '*' in problem or '/' in problem:
return "Error: Operator must be '+' or '-'."
for problem in problems:
for n in problem:
if n not in [' ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '+', '-']:
return 'Error: Numbers must only contain digits.'
if argument2 is not True:
if len(problems) > 4:
return 'Error: Too many problems.'
operator_positions = []
while len(operator_positions) != len(problems):
for problem in problems:
position_operator = 0
for n in problem:
if n in ['+', '-']:
operator_positions.append(position_operator)
break
position_operator += 1
length_numbers = []
for f, problem in enumerate(problems):
x = len(problem[:operator_positions[f]])
length_numbers.append(x)
y = len(problem[operator_positions[f] + 1:])
length_numbers.append(y)
for n in length_numbers:
if n > 4:
return 'Error: Numbers cannot be more than four digits.'
firstline = ''
secondline = ''
dashes = ''
results = ''
for f, problem in enumerate(problems):
right = problem[:operator_positions[f]]
operator = problem[operator_positions[f]]
left = problem[operator_positions[f] + 1:]
firstline += f"{right:>5} "
secondline += f"{operator}{left:>4} "
dashes += "-----"+' '
if argument2:
if operator == '+':
result = int(right) + int(left)
elif operator == '-':
result = int(right) - int(left)
results += f"{result:>5} "
arranged_problems = f"{firstline}\n{secondline}\n{dashes}"
if argument2:
arranged_problems += f"\n{results}"
return arranged_problems
print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Safari/605.1.15
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project