I cannot understand the block below which I put in [[[[[ ]]]]]]]. What does :> do? Thanks
def arithmetic_arranger(problems, *args): # * allows multiple input of items in sequence, Python interpolator.
if len(problems) > 5:
return "Error: Too many problems."
arranged_problems = [ ]
for index, value in enumerate(problems):
#[â32â, â+â, â8â] value is the set to split.
operation = value.split(" ")
if operation[1] not in "-+": # 0 = position 1, 1 = position 2...in Python, so 1st number +/- 2nd number
return "Error: Operator must be '+' or '-'."
if len(operation[0]) > 4 or len(operation[2]) > 4: # both numbers must be less than 4.
return "Error: Numbers cannot be more than four digits."
try: #try-except block to check if all are integers.
value_1 = int(operation[0])
value_2 = int(operation[2])
except ValueError: # A Python keyword to check if all int.
return "Error: Numbers must contain only digits."
#calculate the length of each line
longest_val = max(len(operation[0]), len(operation[2]))
width = longest_val + 2 # the 2 adds spacing to len < 5 above.
#operation = [â32â, â+â, â8â]
#output = f"{operation[0]:>{width}}\n{fâ{operation[1]} {operation[2]}â:>{width}}\n{â-â*width}â
[[[[[[[L1 = (f"{operation[0] :> {width}}â
# " is Python interpolation or selective insertion.
L2 = operation[1] + f"){operation[2] :> {width-1}}â
d = '-' * width ]]]]]]]]] #this block. I can't get it.
try:
arranged_problems[1] += ('' * 4) + L1
except IndexError:
arranged_problems.append(L1)
try:
arranged_problems[2] += (â â * 4) + L2
except IndexError:
arranged_problems.append(L2)
try:
arranged_problems[2] += (â â * 4) + d
except IndexError:
arranged_problems.append(d)
if args:
âââ
#This runs if the second parameter True is passed in denoting we need to calculate the answer value.
âââ
ans = int(operation[0]) + int(operation[2]) if operation[1] == â+â else int (operation[0]) - int(operation[2])
a = fâ{str(ans):>{width}}
try:
arranged_problems[3] += (â â * 4) + a
except IndexError:
arranged_problems.append(a)
output = fâ{arranged_problems[0]}\n
{arranged_problems[1]}\n{arranged_problems[2]}â
output = output + fâ\n{arranged_problems[3]}â
if args else output
return output
# print(arithmetic_arranger([â3 + 855â, â3801 - 2â, â45 + 43â, â123 + 49â]))
# print(â 3 3801 45 123\n+ 855
2 + 43 + 49\n----- ------ ------ -----â)
# print(arithmetic_arranger([â32 - 698â, â1 - 3801â, â45 + 43â, â123 + 49â], True))
# print(â 32 1 45 123\n- 698
3801 + 43 + 49\n----- ------ ---- ------\n -666 -3800 88 172â)