Tell us what’s happening:
Hello, all. I was having an issue where I was having extra spaces at the end of my last problem and that was causing the tests to not be correct. So in order to fix this, I decided to use a for loop to iterate over the last element to strip the last spaces on each line, but for some reason, it won’t work. If someone is able to help me, or understands why this is happening and would be able to explain it to me and possibly help me fix, that would be appreciated
Your code so far
def arithmetic_arranger(problems, show_answers=False):
line1 = ''
line2 = ''
line3 = ''
line4 = ''
if len(problems) > 5:
error = 'Error: Too many problems.'
return error
else:
for problem in problems:
split = problem.split(" ")
num1 = split[0]
operator = split[1]
num2 = split[2]
if operator == '*' or operator == '/':
error = "Error: Operator must be '+' or '-'."
return error
break
if not num1.isdigit() or not num2.isdigit():
error = 'Error: Numbers must only contain digits.'
return error
break
if len(num1) > 4 or len(num2) > 4:
error = 'Error: Numbers cannot be more than four digits.'
return error
break
maximum = max(len(num1), len(num2))
upperbound = maximum + 2
lowerbound = len(num2) + 1
if operator == '+':
answer = int(num1) + int(num2)
answer_str = str(answer)
if operator == '-':
answer = int(num1) - int(num2)
answer_str = str(answer)
line1 += (' ' * (upperbound - len(num1))) + num1 + ' '
line2 += operator + (' ' * (upperbound - len(num2) - 1)) + num2 + ' '
line3 += ('-' * upperbound) + ' '
line4 += ((' ' * (upperbound - len(answer_str))) + answer_str) + ' '
for problem in range(0, len(problems)):
if problem == (len(problems) - 1):
line1.rstrip()
line2.rstrip()
line3.rstrip()
line4.rstrip()
else:
pass
if show_answers == True:
problems = line1 + '\n' + line2 + '\n' + line3 + '\n' + line4
else:
problems = line1 + '\n' + line2 + '\n' + line3
return problems
print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"])))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project