Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Why the answer didn’t come, there’s an error in my codes, can you help me please

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        return 'Error: Too many problems.'
    a = ''
    b = '\n'
    c = '\n'
    d = '\n'

    for number in problems:
        part = number.split()
        #Abbreviation
        n1 = part[0]
        op = part[1]
        n2 = part[2]
        answer = 0

        if op not in ["+", "-"]:
            print('Error: Operator must be "+" or "-".')
        if not n1.isdigit() or not n2.isdigit():
            print('Error: Number must only contain digits.')
        if len(n1) > 4 or len(n2) > 4:
            print('Error: Numbers cannot be more than four digits')
        if op == '+':
            answer = int(n1) + int(n2)
        elif op == '-':
            answer = int(n1) - int(n2)

        ans_len = len(str(answer))
        dash_gap = (2 + max(len(n1), len(n2))) * '-'
        n1_gap = (len(dash_gap) - len(n1)) * ' '
        n2_gap = (len(dash_gap) - max(len(n1), len(n2)))
    ans_gap = (len(dash_gap) - ans_len) * ' '
    gap = 4 * ' '

    a += f"{n1_gap}{n1}{gap}"
    b += f"{op}{n2_gap}{n2}{gap}"
    c += f"{dash_gap}{gap}"
    d += f"{ans_gap}{answer}{gap}"

    if show_answers:
        result = a + b + c + d
    else:
        result = a + b + c 

    return result

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

print this, use print, you are missing quite a lot in your output

Okeyy thanks, but why the progress didn’t complete yet, the output come with correct answer

no, your output is not correct

for print(arithmetic_arranger(["3801 - 2", "123 + 49"])) it shows:

that is not correct.

if you have made changes and still want help, please post your updated code

Tell us what’s happening:

Help, I begginer with this all, can you help me please, how to start to divide the code to 3 form, i tried but it return error, can you help me check what’s wrong with that

Your code so far

def arithmetic_arranger(problems, show_answers=False):

    p1 = ''
    p2 = '\n'
    p3 = '\n\n'
    ans = '\n\n\n'

    part = problems.split()
    n1 = part[0]
    op = part[1]
    n2 = part[2]
    
    if len(n1) > 5:
        return 'Error: Too many problems.'
    if not op '+' or '-':
        return "Error: Operator must be '+' or '-'."
    if not n1.isdigit() or n2.isdigit():
        return 'Error: Numbers must only contain digits.'

    return problems

print(f'\n{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/139.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Do you not see this error in the editor?

Traceback (most recent call last):
  File "main.py", line 15
    if not op '+' or '-':
              ^^^
SyntaxError: invalid syntax

Ouhh ok, tysm for your correction

Tell us what’s happening:

My Formatter Arithmathic
What wrong with this code, can anybody help me please, tysm for your assistance

Your code so far

def arithmetic_arranger(problems, show_answers=False):
   
    #Line
    p1 = ''
    p2 = '\n'
    p3 = '\n\n'
    ans = '\n\n\n'

    if len(problems) >= 5:
            return 'Error: Too many problems.'
    for num in problems:
        #Divide 
        part = num.split()
        n1 = part[0]
        op = part[1]
        n2 = part[2]
    
        #Rules
        if not op is '+' or op is '-':
            return "Error: Operator must be '+' or '-'."
        if not n1.isdigit() or not n2.isdigit():
            return 'Error: Numbers must only contain digits.'
        if len(n1) > 4 or len(n2) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        if op is '+':
            answer = int(n1) + int(n2)
        else:
            answer = int(n1) - int(n2)
        
        #Dash
        dash = '_'*(len(str(answer)) + 2)
        #Line answer
        l_ans = ' '*(len(dash) - len(str(answer)))
        #Line 1 
        l1 = ' '*(len(dash) - len(n1))
        #Line 2 
        l2 = ' '*(len(dash) - len(n1) - 1)

    return problems

print(f'\n{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/139.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Here are some troubleshooting steps you can follow. Focus on one test at a time:

  1. Are there any errors or messages in the console?
  2. What is the requirement of the first failing test?
  3. Check the related User Story and ensure it’s followed precisely.
  4. What line of code implements this?
  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

If this does not help you solve the problem, please reply with answers to these questions.

1 Like

Hi @Faiht

I went ahead and combined your posts for you. In the future, just reply to the original thread to add further updates.

Thanks.

1 Like

Okeyy, tysm for your concern

def arithmetic_arranger(problems, show_answers=False):
   
    #Line
    p1 = ''
    p2 = '\n'
    p3 = '\n'
    ans = '\n'
    if len(problems) > 5:
            return 'Error: Too many problems.'
    for num in problems:
        #Divide 
        part = num.split()
        n1 = part[0]
        op = part[1]
        n2 = part[2]
        
        #Rules
        if not op == '+' and not op == '-':
            return "Error: Operator must be '+' or '-'."
        if not n1.isdigit() or not n2.isdigit():
            return 'Error: Numbers must only contain digits.'
        if len(n1) > 4 or len(n2) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        if op is '+':
            answer = int(n1) + int(n2)
        else:
            answer = int(n1) - int(n2)
        
        #Dash
        dash = '_'*(len(str(answer)) + 2)
        #Line 
        l_ans = ' '*(len(dash) - len(str(answer)))
        #Line 1 
        l1 = ' '*(len(dash) - len(n1))
        #Line 2 
        l2 = ' '*(len(dash) - len(n2) - 1)
        #Gap
        gap = ' '*4
        #Answer
        aof = ' '*(len(dash) - len(str(answer)))

        p1 += f'{l1}{n1}{gap}'
        p2 += f'{op}{l2}{n2}{gap}'
        p3 += f'{dash}{gap}'    
        if show_answers == True:
            ans += f'{aof}{answer}{gap}'
        Final = p1 + p2 + p3 + ans
    return Final
    

print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')


Can you help me this code didn’t work well, the exam didn’t checklist correct them

if you use repr() around the call to arithmetic_arranger you can confront the output with the string shown in the hints


Same condition, how it can’t work ? I think that’s literly complete

Tell us what’s happening:

I’ve tried rpr(), and another solution to complete this Project, but the checklist didn’t correct it until now, can anyone help with with solution please

Your code so far

def arithmetic_arranger(problems, show_answers=False):
   
    #Line
    p1 = ''
    p2 = '\n'
    p3 = '\n'
    ans = '\n'
    if len(problems) > 5:
            return 'Error: Too many problems.'
    for num in problems:
        #Divide 
        part = num.split()
        n1 = part[0]
        op = part[1]
        n2 = part[2]
        
        #Rules
        if not op == '+' and not op == '-':
            return "Error: Operator must be '+' or '-'."
        if not n1.isdigit() or not n2.isdigit():
            return 'Error: Numbers must only contain digits.'
        if len(n1) > 4 or len(n2) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        if op is '+':
            answer = int(n1) + int(n2)
        else:
            answer = int(n1) - int(n2)
        
        #Dash
        dash = '_'*(len(str(answer)) + 2)
        #Line 
        l_ans = ' '*(len(dash) - len(str(answer)))
        #Line 1 
        l1 = ' '*(len(dash) - len(n1))
        #Line 2 
        l2 = ' '*(len(dash) - len(n2) - 1)
        #Gap
        gap = ' '*4
        #Answer
        aof = ' '*(len(dash) - len(str(answer)))

        p1 += f'{l1}{n1}{gap}'
        p2 += f'{op}{l2}{n2}{gap}'
        p3 += f'{dash}{gap}'    
        if show_answers == True:
            ans += f'{aof}{answer}{gap}'
        Final = p1 + p2 + p3 + ans
    return Final
    

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/139.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Please say what specific tests you are not passing and what you have tried to fix them.

Note - the code submitted for these projects must be your own.

1 Like

Use repr() like this:

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

You should see the output in the same format that the tests display.