# \[Solved\] subtraction on my calculator!

**URL:** <https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666>\
**Category:** Questions about Thunkable X\
**Created:** [March 7, 2021, 8:07am UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666 "2021-03-07T08:07:47Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![codeswept](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/codeswept/32/87755_2.png) [@codeswept](https://community.thunkable.com/u/codeswept)\
**Post date:** [March 7, 2021, 8:07am UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/1 "2021-03-07T08:07:47Z")

</div>

I’ve been struggling with making a very basic Thunkable X calculator for an app of mine for the past 3 weeks, and am still stuck.

I finally got the addition part to work, but the others aren’t working.  
SUBTRACTION ERROR:  
It returns  
-(sum of all the numbers inputted)

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/9/b/9b69c152c1ac36d34b34511688be58445d367b93.png)  
Red list-viewer shows all the numbers inputted and the green one shows all the operations.  
The equation I gave for subtraction is 6-1.

MULTIPLICATION ERROR:  
Returns 0

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/d/4/d48b97aee30b9eb191e98bf2d7ebf2359d7ae5ff.png)  
The equation I gave for multiplication is 4\*5.  
DIVISION ERROR:  
Returns 0  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/d/0d4dd9b6262157dc58195608908583f2a197fde2.png)  
The equation I inputted for division is 9/3.

ADDITION SUCCESS:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/8/08b94fb2dad383bf40977706d0adadfa28f75f43.png)  
The equation for addition is 1+1.  
Here’s my code:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/d/0dba085588898824fa651d4254867443e896bf6a.png)  
It’s the same thing if i is not equal to 0, with one change:  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/f/9/f9c1b0549d491a88d3f946c9c22f7696ebd5d858.png)

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [March 7, 2021, 4:11pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/2 "2021-03-07T16:11:18Z")

</div>

It’s really hard to know without seeing more of your blocks, such as what your list input looks like and what the variable “i” stands for. Can you explain what “i” is for here?

How have you verified that the two numbers you think about being added/subtracted/multiplied/divided are actually the two on the screen? Because I would check that before worrying about how to add/etc. them.

My initial guess is that your list contains text values, not numeric values. Especially if you are having the user type in an input as opposed to tapping a button.

My second thought is that maybe the order of the block assignment isn’t happening the way you’d expect it to. So you might try an intermediate step – at least for testing – where you assign a variable to the first number in the list, assign a variable to the second number in the list, and then finally add/etc. the two numbers.

---

<div class="post-metadata">

**Author:** ![codeswept](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/codeswept/32/87755_2.png) [@codeswept](https://community.thunkable.com/u/codeswept)\
**Post date:** [March 7, 2021, 4:13pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/3 "2021-03-07T16:13:01Z")

</div>

Firstly, I’m using buttons here, as shown in my screenshots.

> [@tatiang](#):
>
> So you might try an intermediate step – at least for testing – where you assign a variable to the first number in the list, assign a variable to the second number in the list, and then finally add/etc. the two numbers.

I’m not necessarily using 2 numbers… I have trouble with this app only because it allows users to operate on as many numbers as they want to.

This is what my i is:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/6/26cd8757846dbc64968708270fb0ce36f18b59cd.png)

The code is quite messy, so I can PM you the link.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [March 7, 2021, 5:07pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/4 "2021-03-07T17:07:05Z")

</div>

> [@codeswept](#):
>
> I’m not necessarily using 2 numbers… I have trouble with this app only because it allows users to operate on as many numbers as they want to.

> [@codeswept](#):
>
> The code is quite messy, so I can PM you the link.

Okay. Things definitely get more complicated when you’re trying to do arithmetic on more than two numbers at a time.

---

<div class="post-metadata">

**Author:** ![codeswept](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/codeswept/32/87755_2.png) [@codeswept](https://community.thunkable.com/u/codeswept)\
**Post date:** [March 15, 2021, 8:31am UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/5 "2021-03-15T08:31:15Z")

</div>

Funny thing is, addition works with more than 2 numbers.

**EDIT: I have deleted the project link from my post as the problem is solved by @muneer**

---

<div class="post-metadata">

**Author:** ![catsarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/catsarisky/32/100661_2.png) [@catsarisky](https://community.thunkable.com/u/catsarisky)\
**Post date:** [March 15, 2021, 10:41am UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/6 "2021-03-15T10:41:51Z")

</div>

Your result is consistent with starting from zero and then doing the operation. So adding works, but 6 - 1 is calculated as 0 -6 - 1, and multiplying is 0 \* whatever, and dividing is 0 / 9 / 3.

I’d be looking for the glitch on your logic causing that - is the list starting out with a 0 in it? Or is your handling of the first number wrong?

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [March 15, 2021, 11:36am UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/7 "2021-03-15T11:36:52Z")

</div>

Can you explain the logic of the **=** button. I got confused. Why there is a set of instructions when i = 0 and another set of instructions when i not equal ZERO?

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [March 15, 2021, 11:40am UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/8 "2021-03-15T11:40:04Z")

</div>

Thank you @domhnallohanlon for the correction. 😊

---

<div class="post-metadata">

**Author:** ![codeswept](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/codeswept/32/87755_2.png) [@codeswept](https://community.thunkable.com/u/codeswept)\
**Post date:** [March 15, 2021, 12:01pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/9 "2021-03-15T12:01:42Z")

</div>

That is because the ‘for loop’ is starting from 0. The list wouldn’t be able to get the 0th item of the list, so I’m starting from i+1(which is 1) if the i loop is in its first iteration.

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [March 15, 2021, 12:07pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/10 "2021-03-15T12:07:51Z")

</div>

OK.  
So why you started the loop from ZERO. Is there a reason for that?  
Why not start with ONE and test only the **operators** conditions?

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [March 15, 2021, 12:59pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/11 "2021-03-15T12:59:12Z")

</div>

I made this change and the numbers came correct.  
starting the loop from 1  
before the loop, store the first number in` total so far` variable. /your code is not taking care of the first number.  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/e/5/e5f011745dcc4fceb94427f2638794930030c723.png)

---

<div class="post-metadata">

**Author:** ![codeswept](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/codeswept/32/87755_2.png) [@codeswept](https://community.thunkable.com/u/codeswept)\
**Post date:** [March 15, 2021, 1:52pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/12 "2021-03-15T13:52:17Z")

</div>

Thank you so, so much! This worked. The problem was that I’d initialized my variable total so far to 0, as @catsarisky noticed. Thank you @catsarisky

I’d been working on this calculator for weeks and now it’s finally done!

---

<div class="post-metadata">

**Author:** ![codeswept](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/codeswept/32/87755_2.png) [@codeswept](https://community.thunkable.com/u/codeswept)\
**Post date:** [March 15, 2021, 2:10pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/13 "2021-03-15T14:10:40Z")

</div>

After I added @muneer’s code, it worked a treat!

---

<div class="post-metadata">

**Author:** ![muneer](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/muneer/32/75210_2.png) [@muneer](https://community.thunkable.com/u/muneer)\
**Post date:** [March 15, 2021, 2:12pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/14 "2021-03-15T14:12:36Z")

</div>

Glad to see it working for you.

---

<div class="post-metadata">

**Author:** ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)\
**Post date:** [November 8, 2024, 1:28pm UTC](https://community.thunkable.com/t/solved-subtraction-on-my-calculator/1149666/15 "2024-11-08T13:28:56Z")

</div>


