Check if else code not working

helpwithcode

#1

Ok. First of all, I am making a lie detector. So, the way it works, is that you have your finger scanned. Then, the finger get analyzed. When the print is analyzed, it will randomly set how true the statement you said is. I want the background to be either green or red depending on how true you were. That is where the bug is.


This Is the simple randomizer code. Oh, important to mention, the thing that sets the "done Analyzing" code to 1, then automatically changes it back to 0 to prevent this random value always changing.

This is the first part of I believe the bugged code to set the backgrounds.

....and that is the countinued code. Now, I will show you the bug in action.

As you can see, the truth level is OBVIOUSLY over 50, which means the background needs to be green. Why is it red? I am not a very skilled coder and it took me a bit to put together the code, then THIS happens! I already experienced debugging a "set text to truth" block if you know what I mean. And this bug, just, HELP!


#2

I can't be sure, because I'm uncertain whether you're showing all of your code, but one thing to be aware of that may be affecting you here is something i have trouble with sometimes. That is whether the "true" variable value has actually been calculated, before it is being evaluated in the "if" block. Things that happen simultaneously in hopscotch can be a little tricky. One way to test that would be to introduce a small "wait" block before the "if... then... else..." happens.

If all else fails, sometimes a good way to test your code is to actually set the variable that you are testing to a known value, right before you actually make any decisions based upon it with "ifs" or cormulas or whatever. That is a good way of making sure that your decision process isn't broken. Then, later, after you know the decision process is working correctly, you can delete the part where you are fixing the value and let the variable be assigned some value somewhere else in your code in the normal fashion.

I hope that is helpful.


#3

So a wait block IN the repeat forever or before it? I tried putting a reapeat forever in the reapeat block as the first thing, but it stopped al, the background code from working. No background showed. I don't know about ANYTHING you're talking about in the second paragraph. I'm sorry if I'm frustrating you. I'm really bad at understanding code, especially code like this. Can you just try to explain a bit better?


#4

It's no problem at all. Let me try to break it down just a little bit better.

At some point in time, your code is making a decision based upon whether a certain variable called "Truth" has a value greater than or less than 50. It appears to me that the decision happens after "done analyzing" is set to 1. (By fhe way, you might want to reset "done analyzing" back to zero, once you have checked it and found it to be 1.) But here is the potential problem: what if "done analyzing" gets set to one, and the "if" blocks start trying to make decisions, before your variable "truth" gets assigned a value? We are talking about thousands of a second here.

So, what I am proposing you might try is to put a small delay inside of the "if" block that checks whether "done analyzing" is equal to 1. That would give the other block some time to actually assign a value to your variable, "truth" before the "if, then, else" looks at it. Maybe. Again, I don't know what else is going on in your code.

The other obvious suggestion that I didn't make before is that you could just generate the random number inside of the "if done analyzing" block.

The second part of what I wrote before is kind of what's known as a "diagnostic", which is just a long word for setting up a controlled test. What I was trying to say is that, just for a test, you could set the value of "truth" to be whatever you want, immediately before checking its value in the "if, then, else" block. This way, you know absolutely what the value of "truth" has been set to, and you know exactly when it has happened. The only thing that could go wrong, then, is any logic that follows. So, after doing that little test, once you are happy with the way your logic works in your "if, then, else" block, you could remove the diagnostic that hard codes the value of "truth" and let the other stuff in your program do whatever it wants to with that variable. If this is not really clear, don't worry about it.


#6

Oh, I didn't think of putting the set value random block in the same code, let me try that.


#7

It worked!!! Thank you so much! Finally, I can continue my code (where all I need to do is make it so you can press "Ok" to reset the project.