──────────────────────────────────────

5. Make Code You Can Trust · Write a Helpful Function Signature

Use type annotations to communicate what a function accepts and returns.

Lesson files · lessons/design-and-testing/type-hints

Before you start
- Define a function with parameters and return a calculation.
- Convert values from input() with int().

By the end
- Annotate the parameter and return types of a price function.
- Show where each input string becomes an integer before the function is called.

Where you can use it
Add type hints to a ticket-price helper so another programmer can see which values to pass in and which value to expect back.

A signature shows a function’s name, parameters and return type. Type hints are notes for readers and analysis tools. Python generally does not enforce them at runtime, so validate untrusted input before calling a function that expects certain values.

Predict
Does the annotation points: int itself reject text at runtime?
- No.
- Yes.

Worked example (example.py)

def badge(points: int) -> str:
    return f"{points} points"

print(badge(6))


Example result
6 points

Write price(quantity: int, each: int) -> int. Read two numbers and print their product.

Starter program (starter.py)

def badge(points: int) -> str:
    return f"{points} points"

print(badge(6))


Hint
Convert inputs in the caller and return quantity * each inside price.

Case 1. Try the sample
Input
3
12
Expected output
36

Case 2. No items
Input
0
12
Expected output
0

Stretch
Add a docstring describing the unit used by each.

Reflect
What behaviour still needs a test even with type hints?

Check your prediction
No. An annotation communicates an expectation. Validation is another operation.
