Python - Arithmetic Arranger (Can't print problems side by side)

Since I last posted here I was able to advance in my solution. I had to rewrite the entire code, but now it looks more easy to eye and smart than my previous draft.

However, now I am facing two issues :

first one is:

I want my function to return the solutions side by side. I was able to to come up with a solution to print the problems in a way that they are right aligned. But, the problems are not returned side by side. If someone could shed a light on this for me, I would be super grateful.

the second issue is kinda funny:

When I tell python to return the output (the variable that holds the formatted solution to the problems) it only shows the first problem. However, if I write print(), it shows all the problems.

Here is my code:

def arithmetic_arranger(self):

#---Checks if the length of the list is larger than 5. 
    if len(self)>5:
        return 'Error: Too many problems'        

 #--Beginning of the For Loop   
    for each_index in self:
        each_index = each_index.replace(' ','')
 #------If/else block, the operator will have different values depending on which operator is present in the string.       
        if '+' in each_index:
            operator = '+'
        elif '-' in each_index:
            operator = '-'
        else: 
            return 'Error: Operator must either be + or -'
#-------Assigning the variables to compose the operands and result.        
        n = each_index.index(operator)
        operand_one = each_index[0:n]
        operand_two = each_index[n+1:]
        combinedoperands = operand_one+operand_two
        num1 = int(operand_one)
        num2 = int(operand_two)
#-------Checks if the operands have only digits.
        if combinedoperands.isdigit() == False:
            return ' Numbers must only contain digits'
            break
#-------Checks operands lengths
        if len(operand_one) >4:
            return 'Numbers cannot be more than four digits'
            break
        elif len(operand_two)>4:
            return 'Numbers cannot be more than four digits'
            break
#------Based on the value of the operator, the operation will change.        
        if operator =='+':
            addition = num1+num2
            result = str(addition)
            
        elif operator =='-':
            if num1>num2:
                minus = num1-num2
            else:
                minus = num2-num1
            result=str(minus)
#-------Assigning variables that will compose the display of the problem.            

        left_margin = ' '            
        slashn = '\n'
        whitespace =''
        left_space =''
#------If/else block to adjust the formating of the output depending on the length of the smaller operand.
        if len(operand_one) > len(operand_two):
            difference = len(operand_one)-len(operand_two)
            whitespace = ' '*difference

        elif len(operand_one) < len(operand_two):
            difference = len(operand_two)-len(operand_one)
            left_space = ' '*difference
 #------Assigning variables to compose the output (display of the problem)       
        line_one = left_margin+left_space+operand_one
        line_two = slashn+operator+whitespace+operand_two
        dashes = '_'*len(line_two)
        line_three = slashn+dashes
        line_four= slashn+left_margin+result

#------Here is where I come up with the second issue, regarding the return/print function            
        output = '{0}{1}{2}{3}'.format(line_one,line_two,line_three,line_four)
        print(output)
        # return output

    #If I don't leave this command here, the output will show the value None 
    return ' '

And here is the output:

  600
-3000
______
 2400
 30
+ 7
____
 37
 800
-400
_____
 400

The bottom line is you are going to need to rethink your algorithm for this project. Your algorithm should focus on generating an entire line at a time (i.e. the first operand of each problem would appear on the first line separated by the appropriate number of spaces between).

That is because when a return statement executes, it stops everything it is doing and returns the value specified. This means, the for loop does not continue after the first iteration (hence only the first problem displays.

1 Like

Wow… I think this is gonna be tricky, but I am gonna try it.
The creators of this course deserve a high praise. Three months ago I knew nothing of Python and now I am reading forums, documentation, I even created a github profile so I can update my code through git bash.

This is the first of the five challenges required for the certification but I already feel like I learned a lot.

So I should find a way to only use the return function after all iterations happened.

Thank you for your answer. It is 2 am here where I live, but as soon as I wake up I’m going the editor.

I got it! Now I just have to see if replit is gonna be ok with it