Practice: Summing Prices
Practice lessons combine several earlier ideas in one small task.
weights = [2, 5, 3]
total = 0
for weight in weights:
total = total + weight
print(total)Here is what the example does:
weightscreates a list and stores it inweights.totalstores the number0.- The
forloop takes each item fromweightsone at a time and puts it intoweight. totalcalculatestotal + weightand stores the result.print(total)displays the value stored intotal.
The assignment uses different names or values, but you will follow the same running-total pattern.
Assignment
Complete the starter code so the program does the following:
- Set
pricesto[4, 6, 3]. - Set
totalto0. - Use a
forloop to go through eachpriceinprices. - Inside the loop, update
totaltototal + price. - Print
total.
Your finished program should print exactly this output:
13Be very careful with capitalization, spaces, and line breaks. The output must match exactly.