PYTHON CORE · LESSON 40

Outcome draft

Count the purchases. · about 18 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Gather some knowledge.

The function can go through the list, add the numbers and return the result.

def total(prices):
    result = 0
    for price in prices:
        result = result + price
    return result

✦ So far, the function has just been created.

STEP 2

Hand over the list and show the results.

Create a list of prices, transfer its functions and output the received answer.

prices = [2, 3]
print(total(prices))

✦ If the function from the first step is already created, the answer will be 5.

IN-CLASS PRACTICE

Type the code yourself

Reprint the little project. Keep an eye out for nested indentations.

def total(prices):
    result = 0
    for price in prices:
        result = result + price
    return result
cart = [2, 3]
print(total(cart))
Open the editor for class work
HOMEWORK

Solve it yourself

Create the function total(Zprices): start with result = 0, loop for price in prices add the prices and returns result. Create cart = [2, 3, 4], call total (cart) and output the answer.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework