Arithmetic formatter, help

I’m stuck with the formatter. Looking at the code it should logically work, but it doesn’t, so something must be wrong… :confused:

Please take a look if you have spare time.

Any suggestions very welcome.
Thanks in advance

Did you read through the test output? It will tell you exactly what’s wrong, even if it can be a bit dense to read at first, take it problem by problem.

You’re failing many tests, let’s start with the first one.

arguments = [[‘3801 - 2’, ‘123 + 49’]]
expected_output = ’ 3801 123\n- 2 + 49\n------ -----’

’ 3801 123\n- 2 + 49\n______ _____’
’ 3801 123\n- 2 + 49\n------ -----’

You are using underscore instead of dashes here.

You have to look at it and understand it. Focus on the assert statement, I find it the easiest.

E AssertionError: Expected different output when calling “arithmetic_arranger()” with [“3801 - 2”, “123 + 49”]
E assert “Error: Operator must be ‘+’ or ‘-’.” == ’ 3801 123\n- 2 + 49\n------ -----’

It called your function with this data: [“3801 - 2”, “123 + 49”]

Your output:

Error: Operator must be ‘+’ or ‘-’." ==

What it should output:

3801 123\n- 2 + 49\n------ -----

Run some tests, add some print statements so you can see the contents of each variable at different points so you can trace the problem. You should have a ton of print statements rn.

Please do not take it personally, I know you mean well, and you’re trying to help in your own way, but I’m very frustrated at this point… :frowning:

I know the outputs are not right, that’s obvious. I’ve spent almost a full day trying to find a solution before posting here. I printed everything I could.

I’m trying to learn, but I need help. “Try to figure it out yourself” is not really helping. I don’t have enough skills to debug this.

Could you please tell me where the faulty region of code is ? What’s clashing with each other ? How can a check for " * / " prevent the other parts of the code from working ?

I and others are definitely going to help you, don’t worry.

Programming is all about troubleshooting problems though and pushing through frustration. That’s how we learn. I’ve been there, believe me.

10 passed in 0.03s :metal:

I found solutions to everything, but by trial and error, by crawling through code of others (that’s part of learning for sure).
Not by knowing what was wrong and why :confused: I still don’t understand why the wrong things were wrong.

Sorry, but I don’t know how you paste a bit of code in. :sweat_smile: Please bear with just the text itself.

First: ____________________________________________________________________________________
changing the second optional parameter from

  • rslt=False —> *args

and changing the end bit of code [to show the results line] form

  • “if rslt is True:” or “if rlst:” —> " if (args): "

Solved the problem of the last 2 tests not working. The first solution works for some people, why not me ? For example this guy below. He uses the True, False booleans and it’s working fine.

Creating a list to check for operands solved the problem of “” * \ “” :

  • operatrs=[“+”,“-”]
    if stuff[1] not in operatrs:
    return “Error: Operator must be ‘+’ or ‘-’.”

Why didn’t the below code work to check the string directly as it comes ?

  • for item in problems:
    cut = item.split()
    oper = cut[1]
    if oper != “+” or “-”:
    return “Error: Operator must be ‘+’ or ‘-’.”

Regarding this bit of code:

for item in problems:
cut = item.split()
oper = cut[1]
if oper != “+” or “-”:
return “Error: Operator must be ‘+’ or ‘-

The logic here does not work the way you want:

if oper != “+” or “-”:

It’s not “if oper does not equal + or -” It’s more like “Does oper != + then, is ‘-’ true?”
oper != “+” and “-” are two separate evaluations.

print(bool('-')) is always True, so this line will always execute.

You can try running this:

def test(problems):
for item in problems:
cut = item.split()
oper = cut[1]
print(oper == ‘+’)
print(oper == ‘+’ and ‘-’)
print(oper == ‘+’ or ‘-’)
if oper != ‘+’ or ‘-’:
return ‘Error: Operator must be ‘+’ or ‘-’.’

test([“32 + 698”, “3801 - 2”, “45 + 43”, “123 + 49”])


Error: Operator must be ‘+’ or ‘-’.`

This is simpler:

oper = ‘+’
if oper != ‘+’ or ‘-’:
print(‘Error: Operator must be ‘+’ or ‘-’.’)

Error: Operator must be ‘+’ or ‘-’.

Thanks for explaining . I kind of get it now, I think. …
That’s so counterintuitive though :face_with_raised_eyebrow:

I checked if maybe "if stuff != x or stuff != y" would work instead of "if stuff != x or y" to try and avoid a logical error [since it has two separate questions built in], but it also gives out an error regardless of what’s the operator

What the hell is the call “or” for then ? It seems natural to use it here, but it turns out to fail.

Any ideas as to why the *args worked and x=False didn’t ?

this one doesn’t work:

if oper!="-" or oper!="+" :
    print("1Error: Operator not +-")

but this one does :poop: :

if oper=="*" or oper=="/" :
    print("1Error: Operator not +-")

I don’t get it after all… you can kill me now

Some info of the net in case anyone will have the same issues as me…

If the operands involved in an or operation are objects instead of Boolean expressions, then the Python or operator returns a true or false object, not the values True or False as you could expect. The truth value of this object is determined according to the rules you’ve seen before.

This means that Python doesn’t coerce the result of an or operation to a bool object. If you’re testing two objects using or in Python, then the operator will return the first object that evaluates to true or the last object in the expression, regardless of its truth value:

You should not be printing the error messages

they were printed for debugging, not for actually returning the error for the exercise.

Ok, it’s a common error so I wanted to be sure.

Yes, you were re-using this variable. You use “rslt” as True / False to show the answers.

But then you also use rslt variable to store the actual result line:

rslt=" ".join(line4)

So, if it was True or False, it was getting overwritten here.

I just added a line before the end:


and saw it printed out a row of results instead of True/False:

-666 -3800 88 172 1028

It can help to comment out the testing line to do your own tests and prints (in

# Run unit tests automatically
# main(['-vv'])

Any other variable name would work:

def arithmetic_arranger(problems, answerme=False):

Oooooh that’s what it was !
just me being stupid, and naming two things with the same word :rofl:
I was staring at the code for hours trying to figure out why it doesn’t work for me. I didn’t notice this at all !
Well, at least I’m never making that mistake again… Too embarrassing to do it for a second time :smiley:

And now I also know why the things didn’t work initially.
-the booleans work counterintuitively - it’s “if I match first, I don’t care about the second thing after the OR at all”; not conversational English OR where you check both and then give an answer
-don’t be a mug and track the names I use

1 Like

When I’m coding or troubleshooting I’m always print() ing the variables so I can see how they are changing as the program runs. This is what helped me see this.

Troubleshooting mistakes and dumb typos is basically what programming is. 100% of programmers have been through this and go through this daily.

You’re right about the If/OR statement, I noticed that because I saw it somewhere else recently. Each side of the OR is treated as a separate evaluation.

I’m currently finishing the time/day diplay thingy
As per your advice I printed all the things !
Now I know why python is so proud of making the print statement as simple as it is :grin: People would be furious if it was hard when it’s used so much.

The modulo operator-% is kinda necessary for this new exercise.
The p4e course gave just the bare minimum about it, “it gives back the rest which was left over”. I remembered that and had a hunch that there will be a way to check the amount of days in the skipped hours with left over hours displayed…

The hunch paid off, I found Python Modulo in Practice: How to Use the % Operator – Real Python and they had information about the divmod() this made this test possible.
Well if I had some background in math I probably wouldn’t be surprised. Sadly medical skills don’t trasnfer over to anything :expressionless:

1 Like

divmod() that’s new to me, thanks!