Calc totals from dialog input

How do I get this snippet of code to produce a total sum from the dialog input?

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


--ask how many  were purchased
property TotaItems : {"1", "2", "3"}

set theResult to choose from list TotaItems with prompt "calculate :"
if theResult is false then error number -128
set totals to item 1 of theResult as integer
repeat with i from 1 to totals
	
	display dialog "Enter Cost  " & i & ":" default answer "" with title "Total"
--I want to be able to capture & sum the price for each item based on Totaltems. eg; If --I select 2 items and enter 1.00 and 2.00, 
--I want to sum the total based on how many items selected from 1st dialog box. Don't --know how to accomplsih.
	
end repeat

Try this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--ask how many  were purchased
property TotalItems : {"1", "2", "3"}

set theResult to choose from list TotalItems ¬
   with title ("How Many Items Purchased") ¬
   with prompt ("calculate :") ¬
   OK button name ("Okay") ¬
   cancel button name ("Cancel") ¬
   multiple selections allowed false ¬
   without empty selection allowed

if theResult is false then error number -128

set totals to item 1 of theResult as integer
set costsEntered to {}
repeat with i from 1 to totals
   
   set userResponse to display dialog "Enter Cost  " & i & ":" default answer "" with title "Total"
   set itemCost to text returned of userResponse
   try
      set the end of costsEntered to itemCost as number
   end try
   --√ I want to be able to capture & sum the price for each item based on Totaltems. eg; If --I select 2 items and enter 1.00 and 2.00, 
   --√I want to sum the total based on how many items selected from 1st dialog box. Don't --know how to accomplsih.
end repeat

set costSum to 0
repeat with thisEntry in costsEntered
   set costSum to costSum + (thisEntry as number)
end repeat

return costSum

This works. Thanks for the help

One more ?
How would I capture the value of each cost and store it?
Eg - If i enter 3 costs, how would I capture each input

How would I capture the value of each cost and store it?

The script already does that.

As the costs are entered, they’re stored in a list costsEntered:

		set the end of costsEntered to itemCost as number

so costsEntered is a list that contains the values entered.

The end of the script walks through this list and does the actual sum calculation.

Got it I appreciate the help!
Thanks :smiley: