# Logic not working right

**URL:** <https://forum.makecode.com/t/logic-not-working-right/10085>\
**Category:** micro:bit\
**Created:** [September 17, 2021, 8:31pm UTC](https://forum.makecode.com/t/logic-not-working-right/10085 "2021-09-17T20:31:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonelone](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@tonelone](https://forum.makecode.com/u/tonelone)\
**Post date:** [September 17, 2021, 8:31pm UTC](https://forum.makecode.com/t/logic-not-working-right/10085/1 "2021-09-17T20:31:53Z")

</div>

Hi

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.

> **[oppg.15c #2](https://makecode.microbit.org/60091-02392-36668-03363)**
>
> Made with ❤️ in Microsoft MakeCode for micro:bit.

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!

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [September 17, 2021, 9:15pm UTC](https://forum.makecode.com/t/logic-not-working-right/10085/2 "2021-09-17T21:15:45Z")

</div>

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:

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/1/10fc938c5b1ce1eb2f286b67c687fd472506a3bc.png)

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:

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/2/2cf6621bccf64fed281c7adf28eccc3073c5f633.png)

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.

**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:

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/f/f2002974525c7865802d225fde0ed481482bc27b.png)

---

<div class="post-metadata">

**Author:** ![tonelone](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@tonelone](https://forum.makecode.com/u/tonelone)\
**Post date:** [September 17, 2021, 11:09pm UTC](https://forum.makecode.com/t/logic-not-working-right/10085/3 "2021-09-17T23:09:42Z")

</div>

Hi

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.

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [September 18, 2021, 4:05am UTC](https://forum.makecode.com/t/logic-not-working-right/10085/4 "2021-09-18T04:05:52Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)\
**Post date:** [September 18, 2021, 10:15pm UTC](https://forum.makecode.com/t/logic-not-working-right/10085/5 "2021-09-18T22:15:02Z")

</div>

@tonelne

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_.

Bill

---

<div class="post-metadata">

**Author:** ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)\
**Post date:** [September 20, 2021, 3:31pm UTC](https://forum.makecode.com/t/logic-not-working-right/10085/6 "2021-09-20T15:31:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)\
**Post date:** [September 20, 2021, 3:31pm UTC](https://forum.makecode.com/t/logic-not-working-right/10085/7 "2021-09-20T15:31:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [September 20, 2021, 3:31pm UTC](https://forum.makecode.com/t/logic-not-working-right/10085/8 "2021-09-20T15:31:11Z")

</div>

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`.

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/3/3b43e5d4a02d51534142d1ded04c151f0b519016.png)

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:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/0/01f16db4e1102f5660a21b13d1377de447d1b988.png)

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

---

<div class="post-metadata">

**Author:** ![tonelone](https://avatars.discourse-cdn.com/v4/letter/t/b38774/32.png) [@tonelone](https://forum.makecode.com/u/tonelone)\
**Post date:** [September 22, 2021, 5:55pm UTC](https://forum.makecode.com/t/logic-not-working-right/10085/9 "2021-09-22T17:55:01Z")

</div>

Thank you so much for your replies!

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

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