Module 0 · Orientation · Lesson 0.2

The problem template

Meet the problem template: fill in a function's body, return an answer, and let the tests check it. First problem: a trade's notional value.

Hook

What is this trade worth?

A desk buys 100 shares of LABX at 101.25 each. Before the order goes out, the risk system asks one question: how much money is this trade worth? In this lesson you write the code that answers it.

Concept

Filling in a function

From now on, each problem arrives as a function to fill in. A function is a named piece of code that receives some values, works out an answer and hands it back. Here is a small one that works out a broker’s fee of 0.005 per share:

def fee(quantity):
    return quantity * 0.005

print(fee(1000))

Press Run and it prints 5.0. Read it line by line:

  • def fee(quantity): names the function fee and says it receives one value, called quantity.
  • return quantity * 0.005 works out the answer and hands it back. * is Python’s multiplication sign.
  • fee(1000) calls the function with quantity set to 1000, and print shows the answer it returns.

Treat def and return as a fixed frame for now: you write the line in the middle. Module 4 explains functions properly.

How the tests work. Each problem has test cases: inputs with known answers. Run calls your function on the examples in the problem and shows what it returned. Submit also runs hidden cases, such as a trade of zero shares. If one fails, it shows you the case’s name, so you know what to think about next.

Worked example

One function, many trades

A function is written once and called as often as you like. The same fee works for any order size:

def fee(quantity):
    return quantity * 0.005

print(fee(200))
print(fee(10000))
print(fee(0))

It prints 1.0, 50.0 and 0.0. Each call sets quantity to a new value, and the line in the middle works out that order’s fee. The tests call your function in exactly this way, once for each case.

Check

Predict the output

Problem

Trade notional

notional(quantity, price) -> float

A trade’s notional is its total value: the number of shares times the price of one share. Buying 100 shares of LABX at 101.25 is a trade worth 10,125.00.

Fill in notional(quantity, price). It receives the number of shares and the price of one share, and it returns the value of the trade.

quantity price returns
100 101.25 10125.0
250 40.0 10000.0

Constraints: quantity is a whole number from 0 to 1,000,000; price is a positive number.

Solve it in the editor

Hints

Stuck? Open one hint at a time

Hint 1 · Nudge

Everything you need is already in the two names the function receives: quantity and price.

Hint 2 · Approach

The value of the trade is one multiplication. Python writes “times” as *.

Hint 3 · Pseudo-code
return quantity times price

Solution

How to solve it

Run your code at least once to unlock the solution.

Recap

Three things to keep

  • A problem arrives as a function to fill in; `return` hands your answer back to the tests.
  • `*` multiplies numbers: a trade's notional is its quantity times its price.
  • Run checks the visible examples; Submit also runs hidden cases and names the one that fails.