CBuilderLab· about 30 min· in the browser
Lab: your first Python functions
Write three small functions with if/elif/else and return values, and get all tests green.
Teaches: Python — the basics
Theory
A function takes values in, does something and returns an answer with return. The tests call your functions with different inputs and compare with the answer key — just like a real codebase.
Sub-tasks
- betyg(poang) — grade — Return 'A' for 90 or more, 'B' for 75–89, otherwise 'C'.
- moms(pris) — VAT — Return the VAT (25 %) on a price.
- ar_jamnt(n) — is even — Return True if the number is even.
The starter code
runs in your browser"""Skriv dina funktioner här. Kör testerna tills allt är grönt."""
def betyg(poang):
# TODO: 'A' vid 90 eller mer, 'B' vid 75-89, annars 'C'
...
def moms(pris):
# TODO: 25 % av priset
...
def ar_jamnt(n):
# TODO: True om n är jämnt
...
You write the code; tests you cannot see decide whether it holds up. Create a free account to run the lab.
Try the diagnosticCreate a free accountExpected results
All 6 tests green. betyg(90) → 'A', betyg(75) → 'B', betyg(74) → 'C'; moms(200) → 50.0; ar_jamnt(4) → True.
Common mistakes
- Forgot
return(the function returns None). - Wrong boundaries:
>= 90, not> 90. - Writes
printinstead ofreturn.