I am doing the assignment project but it’s not giving me a pass except for the error codes.
When I get them to print it shows me the exact same outputs as the test is asking, but I still don’t get pass for this.
I’ve finished the codes about a few days ago but I’m spending so many hours to get this to work, and when I press get help to create a topic the pop-up just disappears without even creating the topic so I had to just copy and paste the whole code below.
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
err1 = 'Error: Too many problems.'
return err1
for i in problems:
num = i.split()
if not num[0].isdigit() or not num[2].isdigit():
err2 = 'Error: Numbers must only contain digits.'
return err2
elif num[1] not in ['-', '+']:
err3 = "Error: Operator must be '+' or '-'."
return err3
elif len(num[0]) > 4 or len(num[2]) > 4:
err4 = 'Error: Numbers cannot be more than four digits.'
return err4
line1 = ''
line2 = ''
line3 = ''
line4 = ''
for i in problems:
num = i.split()
if len(num[0]) > len(num[2]):
ml = len(num[0])
line1 += (f"{int(num[0]):>{ml+2}}" + " ")
else:
ml = len(num[2])
line1 += (f"{int(num[0]):>{ml+2}}" + " ")
for i in problems:
num = i.split()
if len(num[0]) > len(num[2]):
ml = len(num[0])
line2 += (f"{num[1]}" + " " + f"{int(num[2]):>{ml}}" + " ")
else:
ml = len(num[2])
line2 += (f"{num[1]}" + " " + f"{int(num[2]):>{ml}}" + " ")
for i in problems:
num = i.split()
if len(num[0]) > len(num[2]):
ml = len(num[0])
line3 += ("-" * (ml + 2) + " ")
if num[1] == '+':
a = int(num[0]) + int(num[2])
line4 += (f"{a:>{ml + 2}}" + " ")
else:
a = int(num[0]) - int(num[2])
line4 += (f"{a:>{ml + 2}}" + " ")
else:
ml = len(num[2])
line3 += ("-" * (ml + 2) + " ")
if num[1] == '+':
a = int(num[0]) + int(num[2])
line4 += (f"{a:>{ml + 2}}" + " ")
else:
a = int(num[0]) - int(num[2])
line4 += (f"{a:>{ml + 2}}" + " ")
if show_answers == True:
problems = line1 + '\n' + line2 + '\n' + line3 + '\n' + line4
else:
problems = line1 + '\n' + line2 + '\n' + line3
return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')