Scientific Computing with Python Projects - Arithmetic Formatter

I 'm having troubles when it comes to print the problems in the correct display. Because the problems should be printed side by side, right aligned and the length of the dash should be the same as of the problems. Also the operator should have a space between the longest operator.

I was able to do it with smaller numbers or larger numbers, but this would require me to manually set the printing parameters to fit it. I believe that there must be a way of making a smarter command but I just can’t seem to figure it out. I came down to these two alternatives:

**Code here:** 

#This is the tricky part, because I can't set this command in a way that centers all the elements while maintaining the space.         
            print('{operand_one}\n{operator:}{operand_two}\n{line}\n{result} '.format(operand_one = operand_one, operator = operator, operand_two=operand_two, line='_',result=result ))
            # This was my first attempt before reading into the formar() method.
            print(' ', num1, '\n', '+', num2, '\n______\n ', result, "\n", sep='')

And here is the full code:

def arithmetic_arranger(self):


    for i in self:
        # Removes blank space before continuing
        i = i.replace(" ", "")
        #Checks if there are more than 5 problems to the function
        if len(self) > 5:
            print("Error: Too many problems")
            break
        #checks if there's a plus operator. I am working only with the Plus operator for now to see if I can get the problems displayed correctly.
        elif "+" in i:
            n = i.index("+")
            operator = '+'
            # Separates operand1 and operand2 based on the index of the operator
            num1 = (i[0:n])
            num2 = (i[n + 1:])
            '''
            Checks the lengths of digits. For some reason I couldn't add both conditions in a single if statement because the editor skipped the first condition.
            It was something like:
            #if len(num1)>4 and len(num2)>4:
                print(Number cannot contain more than four digits)
            '''
            if len(num1)>4:
                print('Number {0} cannot be more than four digits'.format(num1))
                break
            else:
                operand_one = int(num1)            
            if len(num2)>4:
                print('Number {0} cannot contain more than four digits'.format(num2))
                break
            else:
                operand_two = int(num2)
            #Creates a variable result that sums both operands. 
            result = operand_one+operand_two
            #This is the tricky part, because I can't set this command in a way that centers all the elements while maintaining the space.         
            print('{operand_one}\n{operator:}{operand_two}\n{line}\n{result} '.format(operand_one = operand_one, operator = operator, operand_two=operand_two, line='_',result=result ))
            # This was my first attempt before reading into the formar() method.
            print(' ', num1, '\n', '+', num2, '\n______\n ', result, "\n", sep='')
        
        
        #Added this print statement that shows which parameter has the wrong operator.
        else:
            print("Error:Operator in {0} must be either '+' or '-' ".format(i))
            break
    #I added this empty string because the function kept returning None.
    return " "

Thanks to you in advance.

Challenge:* Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

The problem is asking you to return strings, not print them

1 Like

At first I had written the code with return. But I was getting error messages (unfortunately I don’t remember them) so I changed them to print to avoid it.

But now I tried using the return function and it worked. I got some EOL messages while doin’ it so I think that that was the message I got the first time