Python, properly · Chapter 7 of 42

Loops

By the end of this chapter you can make Python repeat work: a fixed number of times with for and range, until something changes with while, and with control over each round using break, continue and a loop's else. 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

DabbaDrop does the same thing many times a day: pack each tiffin, price each count of rotis, send a rider out again and again until the shelf is empty, check order after order. Rafiq wants to scan the day's orders and stop at the first big one; Farah wants a freebie on every few orders.

Copying a line once per tiffin works for three tiffins:

print("Tiffin 1 packed")
print("Tiffin 2 packed")
print("Tiffin 3 packed")

It does not work for 300, or for a number the user types. A loop runs the same block of code again and again, with a name that changes each round.

What you build: 4 tasks

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

  1. Task 1

    roti_table.py: a price table

    A roti costs 15 rupees. Ask Up to how many rotis? (a space after the question mark), read a whole number, and print one line per count from 1 up to that number. For 3 the check sees:

    Up to how many rotis? 1 roti: Rs 15

    2 rotis: Rs 30

    3 rotis: Rs 45

    One roti is singular; every other count says rotis. The check also tries 1 and 10.

  2. Task 2

    trips.py: a rider's trips

    A rider carries at most 6 tiffins per trip. Ask Tiffins to deliver? (a space after the question mark) and read a whole number. While tiffins are left, print one line per trip: the trip number, how many tiffins go on it, and how many are left. Then print how many trips it took. For 20 the check sees:

    Tiffins to deliver? Trip 1: 6 tiffins, 14 left

    Trip 2: 6 tiffins, 8 left

    Trip 3: 6 tiffins, 2 left

    Trip 4: 2 tiffins, 0 left

    Done in 4 trips

    For 0 it sees: Tiffins to deliver? Done in 0 trips

  3. Task 3

    first_big.py: stop at the first big order

    Rafiq wants the first order of Rs 500 or more today. Ask How many orders? (a space after the question mark), then read that many order totals, one per line, with input and no prompt. A total of 0 is a cancelled order: skip it, print nothing. For a total below 500, print its line: Order #2: Rs 120 (orders are numbered from 1, cancelled ones included). At the first total of 500 or more, print First big order: and stop reading. If there is none, print No big orders today at the end. For 120, 0, 640, 900, the check sees:

    How many orders? Order #1: Rs 120

    First big order: #3, Rs 640

  4. Task 4 · interview question

    Interview: freebies.py, free food every few orders

    Farah's offer: every 3rd order of the day gets a free chaas, every 5th a free jamun, and an order that is both (every 15th) gets a free thali instead. Ask Orders today? (a space after the question mark), read a whole number, and print one line per order from 1. For 5 the check sees:

    Orders today? Order 1

    Order 2

    Order 3: free chaas

    Order 4

    Order 5: free jamun

    Order 15 prints Order 15: free thali. The check tries up to 45 orders.

What you will learn

  • for and range: repeat a known number of times.
  • The loop variable, and adding up a total in a loop.
  • while: repeat as long as a condition is true, and the endless loop.
  • break, continue and the loop's else.

More in First steps

Build "Loops" 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.