Progress saves on this device only. Make a backup code to keep it safe or move it to another phone or computer.
Paste a backup code from another device to carry on where you left off.
Found a bug, got stuck on a ticket, or loved it? Kris reads every message.
Or send it to hello@kralnugames.com
Chef's voice, and picking which voice he uses, live in Settings on the main menu.
Wipes your cleared tickets, hints and ticket code on this device. Make a backup first if you might want it back.
The whole course on one shelf. Every dark code box has a Try it button that sends it to the practice station, so you can run it and change it.
Python runs your file from top to bottom, one line at a time, like a recipe. If a line breaks, it stops there and tells you which line.
print works, Print doesn't. dish and Dish are two different variables.if, loop or function. Use 4 spaces.#. Python skips them. Use them to leave yourself notes.# This is a comment
print("Line one") # comments can sit at the end too
print("Line two")Text goes in quotes, single or double. Python calls it a string.
dish = "fish pie" print(dish.upper()) # FISH PIE print(dish.title()) # Fish Pie print(len(dish)) # 8 characters print(dish[0]) # f, the first character print(dish.replace("fish", "cottage")) print("pie" in dish) # True print(" messy ".strip()) # tidies the spaces print("Ha" * 3) # HaHaHa
Need a quote mark inside text? Use the other kind outside: "86'd".
Put f before the quote and anything in { } gets filled in. You can even do maths inside.
table = 7
covers = 4
print(f"Table {table}, {covers} covers")
print(f"That's {covers * 2} bread rolls")
price = 12.5
print(f"£{price:.2f}") # £12.50, two decimal places| Symbol | Does | Example |
|---|---|---|
+ | add | 3 + 2 → 5 |
- | take away | 3 - 2 → 1 |
* | times | 3 * 2 → 6 |
/ | divide (always a decimal) | 7 / 2 → 3.5 |
// | divide, drop the remainder | 7 // 2 → 3 |
% | just the remainder | 7 % 2 → 1 |
** | to the power of | 2 ** 3 → 8 |
Whole numbers are int, decimals are float. Brackets go first, just like school maths.
print(round(3.14159, 2)) # 3.14 print(abs(-5)) # 5 print(max(4, 9, 2), min(4, 9, 2))
A labelled tub. = means "put this in", not "equals".
main_course.2nd_dish breaks.covers beats x.stock = 10 stock = stock - 3 # now 7 stock -= 2 # shortcut, now 5 stock += 10 # now 15 print(stock)
input() always gives text. Convert it before doing maths.
| Tool | Turns into | Example |
|---|---|---|
int() | whole number | int("5") → 5 |
float() | decimal | float("2.5") → 2.5 |
str() | text | str(7) → "7" |
type() | tells you the type | type(5) → int |
covers = int(input("How many covers? "))
print(covers * 2)| Write | Means |
|---|---|
== | is equal to (two equals signs!) |
!= | is not equal to |
< > | less than, more than |
<= >= | at most, at least |
and | both must be true |
or | at least one is true |
not | flips True and False |
Every comparison gives back True or False. These are called booleans.
temp = 4 print(temp <= 5) # True print(temp > 0 and temp <= 5) # True print(not True) # False
Ends with a colon. The lines underneath are indented 4 spaces. Python runs the first branch that's true, then skips the rest.
temp = 58
if temp < 50:
print("Rare")
elif temp < 60:
print("Medium")
else:
print("Well done")
print("Rest the steak") # not indented, always runsAn ordered row of things in square brackets. Positions start at 0. Negative numbers count from the end.
prep = ["onions", "carrots", "celery"] print(prep[0], prep[-1]) # onions celery prep.append("garlic") # add to the end prep.insert(0, "stock") # add at position 0 prep.remove("celery") # take out by name last = prep.pop() # take out the last one prep.sort() # A to Z print(prep, len(prep)) print(prep[0:2]) # slice: positions 0 and 1 print("onions" in prep)
Repeat steps for each item. range(start, stop, step) makes numbers and stops before the stop.
for dish in ["soup", "pie"]:
print(f"Plate the {dish}")
for n in range(1, 4): # 1, 2, 3
print(n)
for n in range(10, 0, -3): # 10, 7, 4, 1
print(n)
for i, dish in enumerate(["soup", "pie"]):
print(i, dish) # numbers each itemKeeps going while the condition is true. Something inside must change, or it never stops. break jumps out early. continue skips to the next round.
stock = 0
while stock < 10:
stock += 4
print(stock)
n = 0
while True:
n += 1
if n == 3:
continue # skip printing 3
if n > 5:
break # stop completely
print(n)A recipe with a name. def writes it, calling it runs it. Ingredients go in the brackets (parameters). return hands a result back.
def with_vat(price):
return price * 1.2
def greet(name, table=1): # table has a default
print(f"Evening {name}, table {table}")
total = with_vat(10)
print(round(total, 2))
greet("Sam")
greet("Alex", 5)
Variables made inside a function stay inside it. That's called scope.
Keys matched to values, in curly brackets. Like a menu: dish → price.
menu = {"soup": 6, "pie": 12}
print(menu["soup"]) # 6
menu["burger"] = 14 # add a new dish
menu["soup"] = 7 # change a price
print(menu.get("lasagne", 0)) # 0 instead of crashing
print("pie" in menu) # checks keys
for dish, price in menu.items():
print(f"{dish}: £{price}")Put risky code in try. If it fails, the except part runs instead of crashing.
answer = "four"
try:
covers = int(answer)
print(covers * 2)
except ValueError:
print("That's not a number, chef")Errors aren't failure, they're the kitchen telling you what's wrong. Read the last line first, then go to the line number.
| Error | Usually means |
|---|---|
SyntaxError | Typo in the shape of the code: missing bracket, quote or colon. |
IndentationError | Spaces at the start of a line are wrong. |
NameError | A name Python doesn't know. Spelling, or missing quotes on text. |
TypeError | Mixing types, like adding text to a number. |
ValueError | Right type, wrong value, like int("four"). |
IndexError | Asking for a list position that doesn't exist. |
KeyError | Asking a dictionary for a key it doesn't have. |
ZeroDivisionError | Dividing by zero. |
print(it). Pros do this all the time.Once you've cleared the rail, write Python on your own computer. On Linux, Python 3 is usually already installed.
python3 to get an interactive prompt. Type exit() to leave..py, like kitchen.py, using any text editor.python3 kitchen.py from the same folder.