0%
⭐ 0 XPMap
Chapter 3 · Quest 1 of 5 · 10 XP

Calculator Arm

🦾 So far your bot can only talk. Now it gets arms: tools it can use. A tool is just a function, a named block of code you can run again and again.

def greet(name):
    return "Hi, " + name + "!"

print(greet("Sam"))
Hi, Sam!

def makes the function. The names in the brackets are its parameters. return hands an answer back to whoever called it. elif means "else if": try the next test. Your first tool: a calculator.

Instructions

  1. Make a function calc(a, b, op).
  2. When op is "+" return a + b, and when it's "-" return a - b.
  3. Also handle "*" (times) and "/" (divide).
  4. Print calc(12, 4, "/"). It should show 3.0.
💡 Hint

Use if and elif: if op == "+": return a + b, then elif op == "-": and so on. Every line inside the function is indented 4 spaces, and the lines inside each if another 4.

Ask the community

bot.py
Terminal
← BackNext →

Chapter 3: Tools

The full quest map

Talk it out

  1. Read the instruction with the ✗ out loud.
  2. Say what your code does, line by line, as if to a friend.
  3. Find the line where what it does and what you want split apart.
  4. Press Run and read the terminal. Is that what you expected?
  5. Still stuck? Press Ask Pip why, or peek at the solution.
  6. Or ask on the community help board: say which quest, and paste your code.
New badge!

Your bot just got a new part. See it on the quest map.

Name your bot

It levels up as you play, and gets a new part with every chapter.