Arithmetic Formatter "Expected different output..."

Tell us what’s happening:

Hello! I am working on the Arhitmetic Formatter challenge, for the Scientific Computing with Python Certificate. I am stuck with one of the errors shown in the console, which I think also affects the following error (because the error message F alters the format of the next printout). It happens to me that I can’t understand what the first error shown on the console refers to. “Expected different output …”. What can be the reason for this error?

I would be very grateful if someone could help me. I do not know what else to do.

Your code so far

def arithmetic_arranger(problems, displayed = False):
    allowed_operations = {"+": "+", "-":"-"}
    
    if len(problems)>5:
        return "Error: Too many problems."
        
    
    linea = ""
    linec = ""
    linedash = ""
    lineresult = ""
    
    for operation in problems:
        opsp = operation.split()
        a = opsp[0]
        b = opsp[1]
        c = opsp[2]

        if len(a)>4 or len(c)>4:
            return "Error: Numbers cannot be more than four digits."
            
            
        elif b not in allowed_operations:
            return "Error: Operator must be '+' or '-'."
            
            
        elif not a.isdigit() or not c.isdigit():
            return "Error: Numbers must only contain digits."
            
            
        else:
            result = eval(a + b + c,{"__builtins__": {}}, allowed_operations)

            maxvalue = max(len(str(a)),len(str(c)), len(str(result)))
            dash = '-' * maxvalue + '--'

            aligna = " " * (len(dash) - len(a))
            alignc = " " * (len(dash) - len(c) - 1)
            alignresult = " " * (len(dash) - len(str(result)))

            linea += aligna + a + "    "
            linec += b + alignc + c + "    "
            linedash += dash + "    "
            lineresult += alignresult + str(result) + "    "
 
    if displayed == True:
        arrange = linea + "\n" + linec + "\n" + linedash + "\n" + lineresult
    elif displayed == False:
        arrange = linea + "\n" + linec + "\n" + linedash  
    
    arranged_problems = print(arrange)
            
    return arranged_problems

The console:

Your browser information:

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

Challenge: Arithmetic Formatter

Link to my repl.it project:
link

Link to the challenge:

your output is None, your function needs to return a string

print returns None, so you are returnig the None that comes from the print

2 Likes

Oh! Okay! arrange is supposed to be a string already, so I can skip the print and just write:

arranged_problems = arrange

I tried that, but now the console is giving a different error (I think, it’s kind of confussing for me)

Trying to understand this errors is giving me a hard time, I’m sorry :joy: I’m very confussed because is failing in the arrangement but in the first test in the console (and other individual test I made outside this editor), the arrangement works fine.

Thank you for your very fast answer!

the lines that start with- is your (wrong) output, the lines with + is the expected output, and the lines with ? point out the differences

in this case it seems you have extra characters at the end of each line

1 Like

Thank you! I didn’t know that! I fixed the four white spaces in the end with:

arrange = linea.rstrip() + "\n" + linec.rstrip() + "\n" + linedash.rstrip()

After doing that there seemed to be another problem with the spaces between each number on each line, as it showed on the console. I managed to solve it by removing len(str(result)) from the maxvalue calculation, like this:

maxvalue = max(len(str(a)),len(str(c)))

Thank you very very much! This thing was driving me crazy :joy:

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