Scientific Computing with Python Projects - Arithmetic Formatter HELP

Tell us what’s happening:

I wrote my attempt for the “arithmetic formatter challenge” and I think it works quite well. When I put in some example inputs, it returns the desired outputs and error messages.

However when I use the provided test_module.py in the challenge it won’t be accepted.
After reading through the test_module remarks, I think the problem is that the output is expected to be one single string containing multiple “/n” to seperate the different operands and the results.

While my code does everything with four strings.

Unless I have overlooked something, my solution should still count right? Since it obeys all the requirements and error conditions (https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter)

So maybe it still counts as having solved the task?

For those who are interested, I will also post my code below so you can try it out for yourselves, I am actually quite proud of it :blush:

def arithmetic_arranger(list,results=False):
    list1=[]
    list2=[]
    list3=[]
    for item in list:
        item=item.split(" ")
        operand1=int(item[0])
        operand2=int(item[2])
        if operand1 < 10000:
            if item[1]=="+":
              operand2=operand2
            elif item[1]=="-":
              operand2=(-1)*operand2
            else:
                print("Error: Operator must be '+' or '-'.")
                exit()
        else:
            print("Error: Numbers must only contain digits.")
            exit()
        list1.append(operand1)
        if len(list1)>4:
            print("Error: Error: Too many problems.")
            exit()
        else:
            list2.append(operand2)
            list3.append(operand1+operand2)
            continue
    a=""
    b=""
    c=""
    d=""
    x=" "   #define a blank space
    y="-"   #define a strike
    for i in range(len(list1)):
        loop1=len(str(list1[i])) #number of digits of operand 1
        loop2=len(str(list2[i])) #number of digits of operand 2
        loop3=len(str(list3[i]))#number of digits of operand 3
        digit=max(loop1,loop2,loop3)  #largest number of digits between operand 1 and operand 2
        a=a+x*(2+digit-loop1)+str(list1[i])+x*4
        if list2[i]>0:
            b=b+"+"+x*(1+digit-loop2)+str(list2[i])+x*4
        else:
            b=b+"-"+x*(2+digit-loop2)+str((-1)*list2[i])+x*4
        c=c+x*2+y*digit+x*4
        d=d+x*(2+digit-loop3)+str(list3[i])+x*4
    print(a)
    print(b)
    print(c)
    if results==True:
        print(d)
    else:
        print("")

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"],True)

Your browser information:

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

Challenge Information:

Scientific Computing with Python Projects - Arithmetic Formatter

Nope, gotta pass the tests!

You also need to return the string, not print it!

You have all the logic, just gotta fix up the return value!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.