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  not in  "-+":  # 0 = position 1, 1 = position 2...in Python, so 1st number +/- 2nd number
return  "Error: Operator must be '+' or  '-'."

if len(operation) > 4 or len(operation)  >  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)
value_2 = int(operation)
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), len(operation))
width = longest_val + 2  # the 2 adds spacing to len < 5 above.

#operation = [“32”, “+”, “8”]
#output =  f"{operation:>{width}}\n{f’{operation} {operation}’:>{width}}\n{‘-’*width}”

[[[[[[[L1 = (f"{operation  :>  {width}}”
# " is Python interpolation or selective insertion.
L2 = operation + f"){operation :> {width-1}}”
d =  '-' * width   ]]]]]]]]]     #this block.  I can't get it.

try:
arranged_problems  +=  (''  *  4)  +  L1
except  IndexError:
arranged_problems.append(L1)

try:
arranged_problems  +=  (‘  ‘  *  4)  +  L2
except  IndexError:
arranged_problems.append(L2)

try:
arranged_problems  +=  (‘  ‘  *  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)  +  int(operation)  if operation  ==  ‘+’  else int  (operation) - int(operation)

a  =  f”{str(ans):>{width}}

try:
arranged_problems  +=  (‘  ‘  *  4)  +  a
except  IndexError:
arranged_problems.append(a)

output = f”{arranged_problems}\n
{arranged_problems}\n{arranged_problems}”
output = output + f”\n{arranged_problems}”
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”)
``````

Start a new project and try this code:

``````test = "test text"
width = 10
print(f"{test:>{width}}")
``````

Experiment with different values for test and width, and see what the result is.

Try replacing the `>` with a `<` and see how it changes.

You can read more here: https://dev.to/erictleung/print-fixed-fields-using-f-strings-in-python-26ng

Thanks. That helps 1 Like