Python · Level 1 Logic

Loops

Repeat code for every item of a list with a for loop and add up a total step by step.

  • for item in list
  • Building a total in a loop
  • range(n) counts 0 to n-1

A for loop runs its block once for each item. Start a total at 0 and add to it inside the loop.

total = 0
for price in [4, 6, 10]:
    total = total + price
print(total)            # 20

for i in range(3):      # 0, 1, 2
    print(i)

Write cart_total(prices) that adds up all prices with a loop. If the total is 50 or more, take 10% off (total * 0.9).

solution.py
Test Suites Pending
Stdout