Microsoft MakeCode

Logic not working right


I’m very new to coding. I’m taking a class and we got a task where we had to make a game.
The game is that you have to press buttonA and then buttonB to score points.

I spent over 4 hours being beyond frustrated when trying to figure it out and was extremely happy when I found this soloution all on my own.

The only problem with my code is that it changes the number of points by only pressing buttonB and I don’t understand why.
I think the problem lies in logic but I cannot for the life of me see what’s wrong!

Some translations:
antallPoeng = number of points
knappA = buttonA
knappB = buttonB

I have found a simpler soloution that works perfectly without bugs, but I really want to understand why this code isn’t working.

Hopefully some really smart people can help me!

Whenever my students are having difficulty with debugging their code, I always tell them, “play computer.” In other words, pretend you are the computer, and follow your instructions exactly as you have written them.

For your program, when you press the A button, this code runs:


So, the only thing that happens is the variable knappA is changed to on. We can make keep track of that in a table of variables:

variable value
antallPoeng 0
knappA on
knappB off

So, when pressing the A button, nothing in the code changes the score.

Now, let’s see what happens when the B button is pressed:


The first thing that happens is the knappB variable is set to on. Let’s make a note of that in our table:

variable value
antallPoeng 0
knappA on
knappB on

Then, we reach the if statement. If knappA is on and knappB is on, then we run the code in the upper block. If not, then we run the code in the lower block. From our table above, we see that both knappA and knappB are on. So, the upper block runs. In that upper block, we add 1 to andtallPoeng, and we set knappA and knappB to off. Let’s update our table:

variable value
antallPoeng 1
knappA off
knappB off

Now, you try it. What happens if you press the B button a second time? According to your code, when does the score change?

Click on the spoiler below to see my answers.


According to your code, the only time that the score changes is when you press button A first, and then press button B. Every time you press the B button, the values for knappA and knappB are reset to off.

Note that the following code does the same thing while using fewer blocks:



Thank you for your answer!

The point of the game is actually that the score only changes when you press button A first and then button B.

Probably explained in a bad way the first time, sorry about that!

The problem with the code is that the score changes even if you only press button B.

I’ve tested it on the microbit and in the simulator.

I just don’t understany why. The code implies that that’s not suppose to happen.

Oh! You’re absolutely right; I completely misinterpreted your description. My fault, not yours.

And, I agree, I don’t see a reason right away that would allow the score to increment if the player only pressed the B button. But there must be something there if that’s what you’re experiencing.

I’ll dig into your code again on Saturday and get you an answer, though!


In your code on and off are variables. You never initialize on, so it’s initial value is 0 and off is also 0. You’re always setting knapA and knapB to 0 and checking to see if they are 0.

Two suggestions:

  1. Use true and false from the Logic menu rather than the on / off variables.
  2. The loop continually sets both knapA and knapB (now it sets both to off, which is 0). You probably only want to change them when the game ends or starts rather than always.


Oh, you can ignore my second suggestion…I didn’t realize it was a timed game (I didn’t notice the pause 5000).

Here’s a challenge to consider too: Get it to work without knappB (Then maybe without using =…and then get rid of one else).

Ooh! You have a sneaky error that I did not notice at first.

on and off are variables, and you did not give them values. So, they both have a value of 0 (zero).

In on start, give these variables appropriate values. They’ll need to go above the blocks where you assign values to knappA and knappB.


MakeCode Blocks also has built-in values, true and false, that you can use instead. You’ll find those in the Logic drawer of the toolbox. So, your code could look like this, too:

You’d need to adjust the code in your forever loop, too, to use these new values.

Thank you so much for your replies!

I made some edits based on your suggestions and now it works with no problems :smiley:

It’s not easy being a n00b, but I now I am one step further in understanding code!