Tell us what’s happening:
I have completed the project and camparing with the true, there are no mistakes in my output, but I can’t pass the test
Your code so far
def display(first_num, second_num, operator, larger_digit):
transf_num = []
i = 0
for number in first_num:
space = larger_digit[i] - len(number)
while space > 0:
transf_num.append(' ')
space -= 1
transf_num.append(number)
i += 1
if i >= len(first_num):
transf_num.append('\n')
else:
transf_num.append(' ')
formatted_num = ''.join(transf_num)
transf_num = []
i = 0
for number in second_num:
transf_num.append(operator[i] + ' ')
space = larger_digit[i] - len(number) - 2
while space > 0:
transf_num.append(' ')
space -= 1
transf_num.append(number)
i += 1
if i >= len(second_num):
transf_num.append('\n')
else:
transf_num.append(' ')
formatted_num += (''.join(transf_num))
transf_num = []
while i > 0:
index = len(first_num) - i
while larger_digit[index] > 0:
transf_num.append('-')
larger_digit[index] -= 1
if i > 0:
transf_num.append(' ')
i -= 1
formatted_num += (''.join(transf_num))
return formatted_num
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
problems = 'Error: Too many problems.'
return problems
first_num = []
second_num = []
station = []
operator = []
larger_digit = []
index = 0
for problem in problems:
brf_pro = problem.replace(' ','')
digit_num = 0
num_order = 1
for char in brf_pro:
if char.isdigit():
digit_num += 1
if digit_num > 4:
problems = 'Error: Numbers cannot be more than four digits.'
return problems
else:
station.append(char)
elif char == '*' or char == '/':
problems = "Error: Operator must be '+' or '-'."
return problems
elif char == '+' or char == '-':
larger_digit.append(digit_num)
digit_num = 0
operator.append(char)
first_num.append(''.join(station))
station = []
else:
problems = 'Error: Numbers must only contain digits.'
return problems
second_num.append(''.join(station))
station = []
if larger_digit[index] < digit_num:
larger_digit[index] = digit_num + 2
else:
larger_digit[index] += 2
index += 1
larger_digit1 = []
for num in larger_digit:
larger_digit1.append(num)
problems = display(first_num, second_num, operator, larger_digit)
if show_answers:
answers = []
i = 0
while i < len(first_num):
if operator[i] == '+':
mid_answer = int(first_num[i]) + int(second_num[i])
else:
mid_answer = int(first_num[i]) - int(second_num[i])
answers.append(mid_answer)
i += 1
transf_num = []
i = 0
for number in answers:
str_num = str(number)
space = larger_digit1[i] - len(str_num)
while space > 0:
transf_num.append(' ')
space -= 1
transf_num.append(str_num)
i += 1
if i < len(first_num):
transf_num.append(' ')
problems += '\n' + ''.join(transf_num)
return problems
print(f'{arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"])}')
print(' 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project