Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

my code worked as expeected but the answers are printed vertically. How can i make it horizontal?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems)>5:
        print('Error: Too many problems.')
    for items in problems:
        lists = items.split()
        integers = int(lists[0])
        operator = lists[1]
        operation = int(lists[2])
        #print(operator)
        if operator == '+': 
            answer = integers + operation
        elif operator == '-':
            answer = integers - operation
        else:   
            print("Error: Operator must be '+' or '-'.")
        if isinstance(integers and operation, int) ==True:
            pass
        else:
            print('Error: Numbers must only contain digits.')
        if integers>=10000 or operation>=10000:
            print('Error: Numbers cannot be more than four digits.')
        if integers > operation:
            L1 = lists[0].rjust(len(lists[0])+2)
            L2 = lists[2].rjust(len(lists[0])+1)
            L3 = '-' * (len(lists[0])+2)
            L4 = str(answer).rjust(len(lists[0])+2)
        else:
            L1 = lists[0].rjust(len(lists[2])+2)
            L2 = lists[2].rjust(len(lists[2])+1)
            L3 = '-' * (len(lists[2])+2)
            L4 = str(answer).rjust(len(lists[2])+2)
        
        print(L1)        
        print(lists[1]+L2)
        print(L3)
        if show_answers == True:
            print(L4)

arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

There isn’t really any special trick, to make them print horizontally. How would you print few numbers in a single line?

please double check the instructions, you need to return the output not print it

but the function go through the list one by one
I can’t print all the integers first before doing the calculation

Why not? Or rather what would need to be changed in code to be able to do that?

Think of the way an old dot matrix printer prints, line by line.

Think of what’s on each line:

               prob1num1                            prob2num2
prob1operator  prob1num2              prob2operator prob2num2

Break up the variables, organize and print them out the way you need to. You’ll need to print prob1num1 first, some spaces, and then prob2num2 then a \n, right?

but i need to return a string as the answer
how can i join strings to a multiple line string?

You can use + and \n

1 Like
def arithmetic_arranger(problems, show_answers=False):
    F1 = ''
    F2 = ''
    F3 = ''
    F4 = ''
    Final = ''
    if len(problems)>5:
        print( 'Error: Too many problems.')
    for items in problems:
        lists = items.split()
        integers = int(lists[0])
        operator = lists[1]
        operation = int(lists[2])
        #print(operator)
        if operator == '+': 
            answer = integers + operation
        elif operator == '-':
            answer = integers - operation
        else:   
            return "Error: Operator must be '+' or '-'."
        if isinstance(integers and operation, int) ==True:
            pass
        else:
            return 'Error: Numbers must only contain digits.'
        if integers>=10000 or operation>=10000:
            return 'Error: Numbers cannot be more than four digits.'
        if integers > operation:
            L1 = lists[0].rjust(len(lists[0])+2)
            L2 = lists[2].rjust(len(lists[0])+1)
            L3 = '-' * (len(lists[0])+2)
            L4 = str(answer).rjust(len(lists[0])+2)
        else:
            L1 = lists[0].rjust(len(lists[2])+2)
            L2 = lists[2].rjust(len(lists[2])+1)
            L3 = '-' * (len(lists[2])+2)
            L4 = str(answer).rjust(len(lists[2])+2)
#            L2 += operator
        F1 += L1 + '    '
        F2 += operator+ L2 + '    '
        F3 += L3 + '    '
        F4 += L4 + '    '
    if show_answers==True:
        Final = F1 + '\n' + F2 + '\n' + F3 + '\n' + F4
    else:
        Final = F1 + '\n' + F2 + '\n' + F3 

#    print(Final)
    return Final
        
arithmetic_arranger(["3801 - 2", "123 + 49"])

printing the answer is perfectly matched as requested
but it is still incorrect when using the return statement

You can get the string rapresentation if you wrap your output in repr before printing: print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"]))). In this way it is easier to confront with the expected output

Actual:   '  3801      123    \n-    2    +  49    \n------    -----    '
expected: '  3801      123\n-    2    +  49\n------    -----'

you have extra spaces before the new line character