Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

I have done the challenge but i want to check if its correct or now because the console is just showing the same 10 instructions and my prewiew is also not showing anything.
I am using android phone to do this in chrome, so i don’t know if it is happening because of my device…
Im also confused in the if statements
Should i add that parentheses or is is not necessary?
I don’t know how to complete the challanve exactly…

Your code so far

import re 

def arithmetic_arranger(problems, show_answers=False):
    #if problems more than ⁵ then error 
    if (len(problems)>5):
        return 'Error: Too many problems.'

    #if * and % then error and if number operands other than digit then error
    for problem in problems:
        if (re.search('*','/')):
            return "Error: Operation must be '+' or '-'."
        elif (re.search('\D')):
            return "Error: Numbers must only contain digits."
    
        #if number on each side more than 4 digits then error 
        first_number = problem.split("")[0]
        operator = problem.split("")[1]
        second_number = problem.split("")[2]
    
        if (len(first_number)>4 or len(second_number)>4) :
            return "Error: Numbers cannot be more than four digits."

        #variables for the arrangement
        first = ""
        second = ""
        lines = ""
        out = ""

        #for calculation
        output = ""
        if (operator == '+'):
            output = str(int(first_number) + int(second_number))
        else:
            output = str(int(first_number) - int(second_number))

        #for the space and dashes
        length =max(len(first_number),len(second_number)) + 2
        top = str(first_number).rjust(length)
        bottom = operator + str(second_number).rjust(length-1)
        output_align=str(output).rjust(length)
        dash = ""
        for i in range(length):
            dash += '-'
        
        #the space between two questions
        if problem!= problems[-1]:
            first += top + '    '
            second += bottom + '    '
            lines += dash + '    '
            out += output_align + '    '

        else:
            first += top
            second += bottom
            lines += dash 
            out += output_align

        #now to use the show_answers =False condition for adding space in between
        gap =""
        if show_answers:
            gap = first + '\n' + second + '\n' + lines + '\n' + out
        else:
            gap = first + '\n' + second + '\n' + out
        return gap


Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Mobile Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

the console is showing the text for the failed tests
you are failing most of them so they show there

this is a difficult one to do on a mobile device, on a desktop I would tell you to use the browser console and see the more detailed test output there
here the suggestion I would give you is to use repr to print the function output

This is the first failure:

  1. arithmetic_arranger(["3801 - 2", "123 + 49"]) should return 3801 123\n- 2 + 49\n------ ----- .

You could run print(repr(arithmetic_arranger(["3801 - 2", "123 + 49"]))) to see that… you get an error that stop the execution of the function.
That wasn’t what I was expecting, tbh.

That re.search you have there is causing issues.
it says re.error: nothing to repeat at position 0

You may want to review how re.search work and adjust its arguments.

I’m pretty curious how you wrote all of this with a fatal error stopping the whole thing from executing in the 6th line of code.

Did you never test it?

Ok thankyou very much

Yes i did and the preview was not showing any kind of error like syntax error or such
Buf if was showing lots of ^^^^^^^^ and what words
So i just assumed it is supposed to be this way…
English isnt my first language so it is difficulf to understand the instructions…sorry ill try again…