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).
Hi!
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:
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 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: