Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code isnt passing any of the tests an I dont know why when i ran my own tests everything was working with t he example problems given to us in the beginning please help me I do not understand

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    top = []
    middle = []
    dashes = []
    answer_row = []
    answer = ''
    if len(problems)>5:
        return 'Error: Too many problems'
    for problem in problems:
        oper = 0
        op = ''
        i = 0
        t1 = False
        t2 = False
        while i<len(problem) and t1 == False:
            if problem[i] =='+' or problem[i] == '-':
                t1 = True
                oper = i
                op = problem[i]
            i+=1
            if t1 == True:
                parts = problem.split()
                part1 = parts[0]
                part2 = parts[2]
                if part1.isdigit()==False or part2.isdigit() == False:
                    return 'Error: Numbers must only contain digits.'

        if t1 == False:
            return "Error: Operator must be '+' or '-'"
        if (len(part1) or len(part2))>4:
            return 'Error numbers cannot be more than four digits'
        if show_answers == True and problem[oper]=='+':
            answer = str(int(part1)+int(part2))
    
        elif show_answers == True and problem[oper]=='-':
            answer = str(int(part1)-int(part2))
        max_length = max(len(part1),len(part2))+2
        answer_row.append(answer.rjust(max_length))
        answer_row.append('')
        top.append(part1.rjust(max_length))
        top.append('')
        middle.append(f'{op}{" "*((max_length-1)-len(part2)-1)} {part2}'.ljust(max_length))
        middle.append('')
        dashes.append('-'*max_length)
        dashes.append('')
    solved = '    '.join(top) + '\n' + '    '.join(middle) + '\n' + '    '.join(dashes)
    if show_answers == True:
        solved += '\n' + '    '.join(answer_row)
    return solved

print(arithmetic_arranger(["3801 - 2", "123 - 49"]))
print(arithmetic_arranger(["1 + 2", "1 - 9380"],))
print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))
print(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]))
print(arithmetic_arranger(["3 + 855", "988 + 40"], True))
print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Safari/605.1.15

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Can you give an example of the test result and the result output by your project?

Use repr() to compare the test results in detail.

print(repr(arithmetic_arranger(["3801 - 2", "123 - 49"])))
1 Like

The first one is when i did repr()
and the second is after is what i get for my output without the repr() function for these two arrays:
[“3801 - 2”,”123 - 49”] and [“1 + 2”,”1 - 9380”]

1 Like

and here is the one without repr() function

1 Like

And how does this output compare to what the tests say the output should be?

(please share copy/pasted text instead of screenshots)

1 Like

Really sorry about that I am quite new to this here it is the repr() one:

'  3801          123    \n-    2        -  49    \n------        -----    '
'  1             1    \n+ 2        - 9380    \n---        ------    '

Here is my output without repr():

   3801          123    
-   2        -  49    
------        -----    
 1             1    
+ 2        - 9380    
---        ------  

the lines here are the dashes by the way for some reason it looks like that on the forum page please see the screenshot as that what it actually looks like I don’t know why it looks like this here.

1 Like

And how do these compare to what the tests say that the output should be?

1 Like

I’ve edited your post to improve the readability of the code. 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

I dont understand since my output looks like the example output given but the test cases look different

1 Like

You have extra spaces

if you print the strings in the tests you will see that they appear as the example
here I am printing the output from your function and after that the test string, your output has too many spaces

the example always have only 4 spaces between problems, it’s written under “If the user supplied the correct format of problems, the conversion you return will follow these rules:”

1 Like

You can see the test says this:

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

and you’ve shared your result:
' 3801 123 \n- 2 - 49 \n------ ----- '

You can compare them. They need to be exactly the same, including the same number of spaces.

'  3801      123\n-    2    +  49\n------    -----'
'  3801          123    \n-    2        -  49    \n------        -----    '
1 Like

THIS IS MY CODE> I changed it so it matches the test case but the code still doesnt pass the tests, I did it how you wanted using the print function to compare the tests and it still does not work please help me

def arithmetic_arranger(problems, show_answers=False):
    top = []
    middle = []
    dashes = []
    answer_row = []
    answer = ''
    if len(problems)>5:
        return 'Error: Too many problems.'
    for problem in problems:
        oper = 0
        op = ''
        i = 0
        t1 = False
        t2 = False
        while i<len(problem) and t1 == False:
            if problem[i] =='+' or problem[i] == '-':
                t1 = True
                oper = i
                op = problem[i]
            i+=1
            if t1 == True:
                parts = problem.split()
                part1 = parts[0]
                part2 = parts[2]
                if part1.isdigit()==False or part2.isdigit() == False:
                    return 'Error: Numbers must only contain digits.'

        if t1 == False:
            return "Error: Operator must be '+' or '-'."
        if (len(part1))>4 or len(part2)>4:
            return 'Error: Numbers cannot be more than four digits.'
        if show_answers == True and problem[oper]=='+':
            answer = str(int(part1)+int(part2))
    
        elif show_answers == True and problem[oper]=='-':
            answer = str(int(part1)-int(part2))
        max_length = max(len(part1),len(part2))+2
        answer_row.append(answer.rjust(max_length))
        answer_row.append('')
        top.append(part1.rjust(max_length))
        top.append('')
        middle.append(f'{op}{" "*((max_length-1)-len(part2)-1)} {part2}'.ljust(max_length))
        middle.append('')
        dashes.append('-'*max_length)
        dashes.append('')
    solved = '  '.join(top) + '\n' + '  '.join(middle) + '\n' + '  '.join(dashes)
    if show_answers == True:
        solved += '\n' + '  '.join(answer_row)
    return solved
1 Like

How does your output compare to what the tests say the output should be?

1 Like

It looks the same to me when I look at it

1 Like

Here is my output

1 Like

Use repr() there might be newlines or spaces that you can’t see this way.

1 Like

hi i did what you asked and this is the result of it:

1 Like

Can you paste the text instead of a screenshot? Can’t really compare this way.

Compare the way I did in this reply:

I can already see you have an extra space at the end.

1 Like

honestly thank you so much since I am quite new I didn’t know how to properly compare results with tests and ur help made it so uhh easier thanks so much it works now :slight_smile:

2 Likes

can you tell me where the problem is ? I have the right code and I couldn’t pass the tests

1 Like