I am making a canteen menu app, and on the receipt page, it will display the list of items the person has bought. Each item on the list will have its name and price:
Is it possible to extract the numbers from each item on the list and add them up and then display that number in a label to show the total cost of what the person is buying?
Yes, it’s possible. If you already have the prices in a list, you can use the Sum block:
Just make sure your list items are numbers. If they are text strings, you need to first add 0 to each item to convert it to a number. That would be best achieved in a loop that "get"s each list item value and adds zero to it:
Or, if you prefer, you can just loop through the “price list” and add j to a “sum” variable:
In that loop, the strings get converted to numbers because they are added to the sum which is a number (0).
the numbers will not always be 10, 6 and 11 because the items and price of each item will change each day.
Of course! I assumed you’d create a list from your data source values. I just needed some sample values to test.
Hi, I used your code but it is the number is always 27, which is the sum of 10, 6 and 11.
Here is the code:
Yes, because you need to make a list from your data source values rather than using my example which uses static list values that never change.
How are you storing the data for the data list viewer?
Try using this block that you can get from the ‘list’ drawer. Replace all the numbers with the values from your data sources, and add as many as needed. Then you can set the text in a label:
Hope this helps.
The data for the list viewer is entered on another page where the teachers can edit the menu:
I tried this but it just displayed the label as “3”, because it added 1 and 2. If you see my latest response it may give you some more background on my app.
You should replace the values inside the sum block with your own values.
Just reading that message
You need to initialize a variable that is an empty list and will hold your prices. When you program, you need to put data in variables, not just in the place where you will display them.
These videos might be helpful for you to better understand how that works: