Cash Register - My code works perfectly but it doesn't pass the tests

What you are talking about is that you could not make many purchases, which you could.

Nope. You can’t.

Anywho

I’m still dubious about the other two global variables you currently have floating around - those are possible issues with the order of execution of your many helper functions are possibly using stale data. I’d get rid of your remaining two extra global variables that aren’t CID and price.

regStatus and noChange are hard to trace since they are updated as side effects.

Yes I can.

Hmm, but the result is the intended one. Couldn’t it be that the issue is on the testing end?

Nope, you can’t.

No the problem is not the tests. Since your manual testing cannot perfectly replicate the steps taken to test the code, you can’t quite say you ‘manually’ pass the tests. I would still get rid of those two global variables. The fact that some projects abuse global variables heavily is a flaw in the curriculum, imho.

Or you could keep chasing debugging? Your code is pretty complicated for the task, so the debugging will take a while.

Yes I can.

I don’t know how the global variables are a problem, they are pretty easy to track for me. They are only changed before an updateDisplay(), because that function displays something depending on those values, and they’re also only changed when I press the button.

I mean, so my problem is I don’t know the exact steps taken to test the code? When the certification project is only a set of instructions with specific things as conditions and my output is the intended one?

Edit: also, I doubt too much can be fundamentally wrong with my code since all the tests pass (including tests involving the status, needing exact change…), this is literally the only one that fails. It’s probably something smaller.

Nope, you can’t.

Thats exactly the issue. You have to think through every possible logical path and that’s easy to screw up. That’s why those globals are a bad idea in a design.

You don’t have to add more arrows. I saw your picture above. A random CID and price is generated for that test that is designed to trigger then target behavior, and your code doesn’t reach that target behavior. You don’t replicate the behavior on the user story for this random CID and price and this your code fails.

Focusing on your code being ‘perfect’ sorta misses the boat here since you aren’t generating the target behavior when tested. It really looks like regStatus blocks functionality if it’s not correctly set.

If regStatus is only updated in updateDisplay but it’s used in purchase which is called first, how do you get correct behavior?

Yes I can.

Ahh cool thought you must have missed it.

Focusing on the tests being perfect might also not be the best approach no? I know they are not perfect because in this same exact lesson there was an instance where two tests failed for my code, I refreshed the page without changing the code and then only one failed (this one).

Ok, your code is perfect. Enjoy your certificate.

regStatus is not updated in updateDisplay, that function only updates the display to match the data. regStatus is changed when you press the button in the functions purchase and processChange.

Thank you you have been so helpful.

After a drawer is closed, how does it open again when CID changes? It looks like regStatus blocks functionality.

1 Like

I don’t understand the question very well. Do you mean once the status is “CLOSED”? It wasn’t supposed to open again or? The mock project the lesson provides is like that, once it’s closed the app is pretty much bricked. I tried to replicate that.

That’s exactly your issue. Once it’s closed, changing CID has no effect. Because you stuck that status in a global variable. And this is causing your code to fail.

1 Like

Wow that actually worked. It wasn’t because it’s a global variable it’s still a global but it worked! I just had to remove the if statement that bricks my program after it’s closed (if regStatus !== ‘CLOSED’).

I can’t believe this. Thanks!

It’s actually because it’s a global variable with a wrong value in it and your reasoning about it was wrong :slight_smile: this is a great example of how global state variable bugs are confusing and hard to fix.

No it was because I had an if statement that bricks the app and gets in the way of the testing process. My variable is still gloriously global and it passed, and I got my certificate!

That if statement was causing a problem because you were using a global variable with bad data in it.

But hey, you passed the test, which is most important for you! Nice work passing

The cool thing is, now you could probably remove that global variable and make it work in a clearer way.

Thank you very much!

Hi there, I am glad you were able to work things out to your satisfaction.
If I can comment on the discussion you were having above, for the sake of others reading at least:

Jeremy was trying to teach you something that most developers learn in school. Then relearn at work. But I understand why you may be sceptical because of your current level of experience. Here is a discussion on Reddit about global variables though that you may get to understand one day (provided your coding journey goes beyond fCC). I hope you can gather what you can from it now and come back to it several times as your experience grows to solidify any points that didn’t make sense on first reading it.

https://www.reddit.com/r/learnpython/s/akm1X9uABX