Python, properly · Chapter 6 of 42

Decisions

By the end of this chapter your programs can choose: compare values, run one block of code or another, combine conditions with and, or and not, and pick between many cases with match. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First steps
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

Real rules are full of "if": delivery is free above a certain total, the kitchen is closed in the afternoon, a coupon may or may not be typed, and each order status needs its own message for the customer. Bhavna keeps these rules in her head and on sticky notes; she wants them in code, so the counter screen gets them right every time.

Until now every line of a program ran, top to bottom. A program that follows rules has to skip some lines and run others, depending on the values it has.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    delivery_fee.py: three fee bands

    Ask Order total? (a space after the question mark) and read a whole number of rupees. The delivery fee is Rs 40 below 200, Rs 20 from 200 to 499, and free from 500 up. For 150 the check sees:

    Order total? Delivery: Rs 40

    For 650 it sees:

    Order total? Delivery: free

    The check tries the edges too: 199, 200, 499 and 500.

  2. Task 2

    kitchen_open.py: lunch and dinner hours

    The kitchen cooks from 11 until 15 (lunch) and from 19 until 23 (dinner): open at 11 and at 19, closed again at 15 and at 23. Ask Hour (0-23)? (a space after the question mark) and read a whole number. Print Kitchen is open or Kitchen is closed. For 12 the check sees:

    Hour (0-23)? Kitchen is open

    It tries every hour of the day.

  3. Task 3

    coupon.py: no code, a good code, a bad code

    Ask Coupon code? (a space after the question mark). Clean the answer: no spaces at the ends, all capitals. If nothing is left, print No coupon. If it is DABBA10, print 10% off. Anything else prints Unknown coupon: and the cleaned code. For dabba10 the check sees:

    Coupon code? 10% off

    For an empty answer, or only spaces:

    Coupon code? No coupon

    For abc:

    Coupon code? Unknown coupon: ABC

  4. Task 4 · interview question

    Interview: status.py, the or that is always true

    Bhavna's status.py says In the kitchen for every order, even delivered ones. Find out why, and fix it (rewriting it with match is the neat way). Ask Status? (a space after the question mark); the status can have any capitals and spaces around it, as in the order sheet. The messages are:

    placed or cooking: In the kitchen

    out_for_delivery: On the way

    delivered: Delivered, enjoy!

    cancelled: Cancelled

    anything else: Unknown status: and the status in small letters

    For DELIVERED the check sees:

    Status? Delivered, enjoy!

What you will learn

  • Comparisons: ==, !=, <, <=, >, >=, and chaining them.
  • if, elif, else, and how Python picks a branch.
  • and, or, not, and the famous or trap.
  • Truthiness: which values count as false.
  • match: one value against many cases.

More in First steps

Build "Decisions" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.