Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

My code passes all of the error message tests but only one of the formatting tests. As far as I can tell, when manually inputting the input as the test would do it, it produces the correct formatting, but when the test tries it it fails. Am I missing certain things to make it consistent?

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    arranged_problems=[]
    tops=[]
    ops=[]
    bottoms=[]
    ds=[]
    answers=[]
    t1=''
    op1=''
    b1=''
    d1=''
    ans1=''
    t2=''
    op2=''
    b2=''
    d2=''
    ans2=''
    t3=''
    op3=''
    b3=''
    d3=''
    ans3=''
    t4=''
    op4=''
    b4=''
    d4=''
    ans4=''
    t5=''
    op5=''
    b5=''
    d5=''
    ans5=''
    final_arrangement=''
    if len(problems)>5:
        return 'Error: Too many problems.'
    else:
        pass
    for char in problems:
        top_space=0
        bottom_space=0
        exp=char.split(" ")
        # exp=["num1""op""num2"]
        max_length=(max(len(str(exp[0])),len(str(exp[2]))))       
        if max_length>4:
            return ('Error: Numbers cannot be more than four digits.')
        try:
            int(exp[0])
        except:
            return ('Error: Numbers must only contain digits.')
        try:
            int(exp[2])
        except:
            return ('Error: Numbers must only contain digits.')
        if exp[1] not in ["+","-"]:
            return ("Error: Operator must be '+' or '-'.")
        exp=char.split(" ")
        top=exp[0].rjust(max_length+2)
        op=exp[1]
        bottom=exp[2].rjust(max_length+1)
        d="-"*(max_length+2)
        if show_answers:
            if op=='+':
                ans=int(top)+int(bottom)
            else:
                ans=int(top)-int(bottom)
            ans=str(ans).rjust(max_length+2)
        else:
            ans=''
        tops.append(top)
        ops.append(op)
        bottoms.append(bottom)
        ds.append(d)
        answers.append(ans)
    try:
        tops[0]
    except:
        pass
    else:
        t1=tops[0]+'    '
        op1=ops[0]
        b1=bottoms[0]+'    '
        d1=ds[0]+'    '
        ans1=answers[0]+'    '
    try:
        tops[1]
    except:
        pass
    else:
        t2=tops[1]+'    '
        op2=ops[1]
        b2=bottoms[1]+'    '
        d2=ds[1]+'    '
        ans2=answers[1]+'    '
    try:
        tops[2]
    except:
        pass
    else:
        t3=tops[2]+'    '
        op3=ops[2]
        b3=bottoms[2]+'    '
        d3=ds[2]+'    '
        ans3=answers[2]+'    '
    try:
        tops[3]
    except:
        pass
    else:
        t4=tops[3]+'    '
        op4=ops[3]
        b4=bottoms[3]+'    '
        d4=ds[3]+'    '
        ans4=answers[3]+'    '
    try:
        tops[4]
    except:
        pass
    else:
        t5=tops[4]
        op5=ops[4]
        b5=bottoms[4]
        d5=ds[4]
        ans5=answers[4]
    final_arrangement=(f'{t1}{t2}{t3}{t4}{t5}\n{op1}{b1}{op2}{b2}{op3}{b3}{op4}{b4}{op5}{b5}\n{d1}{d2}{d3}{d4}{d5}\n{ans1}{ans2}{ans3}{ans4}{ans5}')
    return final_arrangement


print(f'\n{arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)}')

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

It would really help if your variable names used real words so the code is human readable.

This is probably your problem. You likely have extra whitespace an the end of rows.

All this try-pass is suspicious though. You should not need any try-pass for this project.

what do you mean by extra whitespace? and I couldn’t find another way to bypass the index error without using the try

it now works for all the test with show answers set to true

Check the dev tools console (Press the f12 key to access) and it will have a more detailed error message where you can see the whitespace more clearly.

You should structure your code so you don’t even try to have an index error.

It works now, the issue was an extra \n before the answers when it was false. I used the try because I got an index error when trying to format expressions that were less than 5 problems long. What’s the most elegant way of doing it?

Why index in at all?

I was indexing to find the correct top number, operator and bottom number to change the order so it fit with the formatting and corresponded on the output, e.g. 2 problems so it can’t give values to the 3rd 4th and 5th set of variables for the formatted problem and that’s when it threw the error so I used try to go around it.

You don’t need to manually index in, and even if you do, Python knows how long that array is

Im confused as to what you’re asking me now sorry, I just tried to use functions I recognised that I knew what they did.

  1. throw out the idea of using try-catch

Do you know how to check how many things are in the tops array?

using len(tops) right?

That would do it. So if you know how long that array is, you shouldn’t bother trying to index past the end of the array.

ah ok so rather than try, use if statements regarding the length

Why use if statements? Why not a loop?

could you use a loop in range(len(problems)) ?

Sounds like something that would work

thanks so much i’ll try and make my code more concise and readable next time :slight_smile:

2 Likes

Once you have something working, its totally worth coming back to rework the code for readability and simplicity. That’s what we do on the job all the time.

1 Like