Tell us what’s happening:
Describe your issue in detail here.
The problem i’m having is in the last test template, more specifically, [test_five_problems_with_solutions]. The problem is that is not displaying correctly in the output, i don’t know what to do right now. If any of you could help me, i’d be thankful.
Your code so far
def arithmetic_arranger(problems, solve = False):
first = ""
second = ""
third = ""
fourth = ""
arranged_problems = ""
for item in problems:
#first error
if len(problems) > 5:
return "Error: Too many problems."
break
#Slitting using split function
itemh = item.split()
# x = [a + b] ---> ['a', '+', 'b']
#Second error
if itemh[1] == "+":
op = itemh[1]
elif itemh[1] == "-":
op = itemh[1]
else:
return "Error: Operator must be '+' or '-'."
break
#Third error
try:
n1 = int(itemh[0])
n2 = int(itemh[2])
except:
return "Error: Numbers must only contain digits."
break
#Fourth error
if len(itemh[0]) > 4 or len(itemh[2]) > 4:
return "Error: Numbers cannot be more than four digits."
break
#bar identation
if len(itemh[0]) + 2 > len(itemh[2]) + 2:
bi = len(itemh[0]) + 2
else:
bi = len(itemh[2]) + 2
#top, mid, bottom (numeros em cima, meio e embaixo)
top = str(n1).rjust(bi) #alinha um valor a direita
mid = str(n2).rjust(bi-1)
strip = '-' * bi
#bot (resultado)
if op == "+":
result = n1+n2
else:
result = n1-n1
bot = str(result).rjust(bi)
#first, mid, bot
if item!=problems[-1]:
first += top + " "
second += op + mid + " "
third += strip + " "
fourth += bot + " "
else:
first += top
second += op + mid
third += strip
fourth += bot
#final (juntando tudo)
if solve:
arranged_problems = first + '\n' + second + '\n' + third + '\n' + fourth
else:
arranged_problems = first + '\n' + second + '\n' + third
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/104.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: