Here is my code for the exercise:
def arithmetic_arranger(problems, show_answers=True):
"""problems is a list of strings"""
first_row = [] # implemento le righe come liste
second_row = []
separator_row = []
result_row = []
for pb in problems:
addend_list = pb.split(' ') # separo numeri e segno
addend_1 = addend_list[0]
sign = addend_list[1]
addend_2 = addend_list[2]
row_length = max(len(addend_list[0]),len(addend_list[2])) + 2
first_row.append(' '*(row_length-len(addend_list[0])) + addend_list[0]) #prima riga
second_row.append(sign + ' '*(row_length-len(sign)-len(addend_2)) + addend_2) #seconda riga
separator_row.append('-'*row_length) #riga separatrice
if show_answers: # controllo per visualizzazione risultato
if sign == '+':
result = int(addend_1) + int(addend_2)
elif sign == '-':
result = int(addend_1) - int(addend_2)
result_row.append(' '*(row_length-len(str(result))) + str(result))
# Unire gli elementi di ciascuna lista con quattro spazi e stampare le righe risultanti
print(' '.join(first_row))
print(' '.join(second_row))
print(' '.join(separator_row))
if show_answers:
print(' '.join(result_row))
arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])
So my code works properly since it display the correct formatting, however something seems to be wrong and I cannot see what since indeed the requested solution is identical to mine. Can anyone help me?
Thanks in advance,
Riccardo
Edit: okay I need to handle errors first , I did not recognise error handling was missing.