Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

i can’t pass the test even if my code works, and also i used repr()
can anyone help?

Your code so far

def arithmetic_arranger(problems, display_answers=False):
    # error with * and /
    first_numbers = []
    second_numbers = []
    operators = []
    answers = []

    for problem in problems:
        if '+' in problem:
                operator = '+'
                parts = problem.split('+')
        elif '-' in problem:
                operator = '-'
                parts = problem.split('-')
        else:
                raise ValueError("Error: Operator must be '+' or '-'.")

        first = parts[0].strip()
        second = parts[1].strip()

        #error with 4 digits
        if len(first) > 4 or len(second) > 4:
            raise ValueError("Error: Numbers cannot be more than four digits.")

        first_numbers.append(first)
        second_numbers.append(second)
        operators.append(operator)

    #error with digits
    alowed_chars=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', ' ']
    for problem in problems:
        for digit in problem:
            if digit not in alowed_chars:
                raise ValueError('Error: Numbers must only contain digits.')
   

    


    # Check digit lengths
    if not first.isdigit() or not second.isdigit():
         raise ValueError("Error: Numbers must only contain digits.")
    
    #lot of problems
    if len(problems)>5:
        raise ValueError('Error: Too many problems.')
    
    #output
 
    top = []
    bottom = []
    dashes = []
    answer_row = []


    for i in range(len(problems)):
        width = max(len(first_numbers[i]), len(second_numbers[i])) + 2
        top.append(first_numbers[i].rjust(width))
        bottom.append(operators[i] + ' ' + second_numbers[i].rjust(width - 2))
        dashes.append('-' * width)
        if display_answers:
            if operators[i] == '+':
                answer = str(int(first_numbers[i]) + int(second_numbers[i]))
            else:
                answer = str(int(first_numbers[i]) - int(second_numbers[i]))
            answer_row.append(answer.rjust(width))
    
    spaces=('    '.join(top) + '\n' + '    '.join(bottom) + '\n'+'    '.join(dashes))

    if display_answers:
        spaces = spaces + ('\n' +'    '.join(answer_row))
    return spaces





    
    










Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Welcome to the forum :wave:

Can you share an example of your output with repr() and how it compares to the test output?

1 Like

i pass all the tests except 5 6 7 8

I can’t really tell what I’m looking at here.

Please share, in text format (not a screenshot)

  1. your output
  2. the test required output

Using repr() as you have done above. You’ll need to clearly label each.

Try to think about how to present what information is needed and how to present it in a legible way. Not only will this help people to help you on the forum, it will help you solve the problem yourself.

1 Like

i just want to know if the problem is in my code or something else

1 Like

I’m new at those type of projects so i find a difficulty to understand what the tests are looking for

1 Like

I get that.

The test tells you exactly what it’s looking for:

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

It’s in a raw string format though, where you can see newline characters. You can use repr() to show the result of your function like this

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

Then you will be able to compare your results, and what the test says the results should be.

1 Like


yes i tried but the tests 5, 6, 7, 8 does not pass i don’t know why

1 Like

What does test 5 say?

And what does your function return when you test it?

1 Like

the test 5 says that when i enter more than 4 problems it should raise an error, and when i did it, my code show me the error, which means that my code is working, but not passing the test

You need to take the tests extremely literally. It actually does not say you should raise an error. It says your function should return a string. You need to make sure what your function returns is exactly, word for word, spaces and punctuation what is written.

def arithmetic_arranger(problems, display_answers=False):
# error with * and /
first_numbers =
second_numbers =
operators =
answers =

for problem in problems:
    if '+' in problem:
            operator = '+'
            parts = problem.split('+')
    elif '-' in problem:
            operator = '-'
            parts = problem.split('-')
    else:
            print("Error: Operator must be '+' or '-'.")

    first = parts[0].strip()
    second = parts[1].strip()

    #error with 4 digits
    if len(first) > 4 or len(second) > 4:
        print('Error: Numbers cannot be more than four digits.')

    first_numbers.append(first)
    second_numbers.append(second)
    operators.append(operator)

#error with digits
alowed_chars=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', ' ']
for problem in problems:
    for digit in problem:
        if digit not in alowed_chars:
            print('Error: Numbers must only contain digits.')

#number of digits
if len(first) > 4 or len(second) > 4:
print(‘Error: Numbers cannot be more than four digits.’)

#lot of problems
if len(problems)>5:
    print('Error: Too many problems.')

#output

top = []
bottom = []
dashes = []
answer_row = []


for i in range(len(problems)):
    width = max(len(first_numbers[i]), len(second_numbers[i])) + 2
    top.append(first_numbers[i].rjust(width))
    bottom.append(operators[i] + ' ' + second_numbers[i].rjust(width - 2))
    dashes.append('-' * width)
    if display_answers:
        if operators[i] == '+':
            answer = str(int(first_numbers[i]) + int(second_numbers[i]))
        else:
            answer = str(int(first_numbers[i]) - int(second_numbers[i]))
        answer_row.append(answer.rjust(width))

spaces=('    '.join(top) + '\n' + '    '.join(bottom) + '\n'+'    '.join(dashes))

if display_answers:
    spaces = spaces + ('\n' +'    '.join(answer_row))
return spaces

print(repr(arithmetic_arranger([“3801 - 2”, “123 + 49”])))

I changed my code but still i face the same problem

1 Like

i finallt knew my fault, it was the raise error that i should replace it with a return, Thank you so much for helping me, and sorry if i cause you problems.

2 Likes

I am having issue with the tests, which are failing somehow.
but when i am running the same test data on the function i created i am getting the right error messages:

hi @abhishekg00715 , please create your own topic, we are unable to help you in someone else’s topic

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.