Arithmetic formatter

I’m not sure what I am doing wrong in my code. Any help is appreciated !

It seems your system is putting out error messages when there is no error. Also your error message isn’t exactly what they asked for:

When you review your output, read through it and it will tell you what the problem is.

I’ll try that thanks

Cool, let us know if you’re able to resolve the errors or if you still can’t find the problem. The outputs you’re seeing are the test assertions like you worked with in the Chai Mocha course. It tells you why it failed, and how your app output differs that from what it was supposed to.

Take this one for example.

fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'

So the error states it was expecting different output. Then it proceeds to tell you what your app give, and what it was supposed to give.

assert actual == expected_output, fail_message

assert 'Error: Numbers cannot be more than four digits.' == (' 11 3801 1 123 1\n'\n '+ 4 - 2999 + 2 + 49 - 9380\n'\n '---- ------ --- ----- ------')

That above message is asserting that your output was supposed to be == to expected output. In this case your output was Error: Numbers cannot be more... while the expected output should have been the formatted problems. It seems your code gave error and error message even when the problem is actually formatted correct.

Lastly it shows all the output lines. E indicates an error on that line. + indicates lines that were there that weren’t supposed to be, while - indicates lines that were supposed to be there but were missing.

E         + Error: Numbers cannot be more than four digits.
E         -   11      3801      1      123         1
E         - +  4    - 2999    + 2    +  49    - 9380
E         - ----    ------    ---    -----    ------

So it says your output added the error line, but was missing the problem lines.

Hope that clarifies a bit.

1 Like

I haven’t been able to figure out how to fix my code. Could you take a look at it again please?

Well, right now according to the output from the tests, one of your problems is that your code keeps giving the error “Error: Numbers must only contain digits.” even when the entry is valid:

So, when you run your function with ["3801 - 2", "123 + 49"] it should print the proper answer, but yours is giving the error message, so focus on that error first. As that occurs on all inputs that needs fixed first.

Seems there might be an issue with how you are checking to see if the problems only contain numbers. What exactly is “.isdigit()”?

‘.isdigit’ is a true or false way to check if the string provided contains digits or not

Oooppps, haha… was thinking in Javascript. But the first part of my statement is correct. Why do you think your program is always printing the error “Error: Numbers must only contain digits”???


So in this statement, what happens if both of your numbers are digits?

I tried changing it to ‘if not first_num.isdigit() or not second_num.isdigit():’
But that ends us messing things up more

No, not messing things up more… progressing. I mean this line is definitely wrong… right now you had it set if either number is a digit to give an error, but thats wrong. You want it to give the error only if either is not a digit, so your change is correct.

Whats happening after that is your now seeing additional errors you need to address.

What errors are you seeing after you correct the isdigit error?

So I put in your correction:

if not first_num.isdigit() or not second_num.isdigit():

And your code definitely gets further after that. The next error now on the console has to do with your formatting. The first example is when given problem: ["3801 - 2", "123 + 49"]. It tells you the correct answer should look like this:


The minus sign in front means that’s something missing that should be there. Things with + signs are things your code added. So instead what your code is printing is:


So you can see the problems. The spacing is wrong, the signs are wrong, and for some reason your application is printing the second equation multiple times instead of printing the first equation and then the second equation. So there is something also wrong with how you’re reading and printing problems.

You’ll need to go through your problem handling code and try to find why its giving this incorrect output.

I would also recommend to take the time to familiarize yourself with how to read these error messages. They are pretty straight forward about telling you what the problem is once you know what to look for, and being able to identify problems with code is in my opinion just as important as writing code.

1 Like

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