Arithmetic Formatter Noobie

Tell us what’s happening:
I cant seem to get the problems side by side, ive look at many articles and I still cant see what I am doing wrong. I know the code is correct, besides the fact that the problems do go side by side

Your code so far

def arithmetic_arranger(Mproblems, solver= False):
    fnum_list = ''
    operators_list = ''
    snum_list = ''
    lines = ''
    solution = ''

# Limiting the problem to just 5
    if len(Mproblems) > 5:
        print('Error: Too many problems.')
#Loop
    for problems in Mproblems:
        problems = problems.split()
        fnum_list = problems[0]
        operators_list = problems[1]
        snum_list = problems[2]
#Check if its a digit & Check the length of number is not greater than 5
        if fnum_list.isdigit() and snum_list.isdigit():
            if len(fnum_list) > 4 or len(snum_list) > 4:
                print('Error: Numbers cannot be more than four digits.')
#Checking + or - only
            if operators_list == '+':
                solution = int(fnum_list) + int(snum_list)
            elif operators_list == '-':
                solution = int(fnum_list) - int(snum_list)
            else:
                print('Error: Operator must be + or - ')
#Defining the distance
        distance = max(len(fnum_list) , len(snum_list)) + 2
#Structure of Math problems
        topnum = str(fnum_list.rjust(distance))
        botnum = str(operators_list.ljust(0)) + ' ' + str(snum_list.rjust(distance - 2))
        solution_line = str(solution)
        lines = '-' * distance
#Chekcing solver
    if solver:
         solution = topnum + "\n" + botnum + "\n" + lines + "\n" + solution_lin
         return solution
    else:
        solution = topnum + "\n" + botnum + "\n" + lines
        return solution
    print(solution)

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

Arithmetic Formatter - Replit
Here is the proper layout of the code

Arithmetic Formatter - Replit

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

As you use these variables in your final result, you need to keep adding to these variables during each iteration

Did you solve your problem? your ‘operations’ have too much ‘\n’

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.