Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

A Plan Before Code

Ask an assistant for a whole program, and you get it all at once. Asked for Mia's whole report, with the program attached, one answer wrote all of it in one go. It was right, but it worked out what Mia earned with sum(walk_price(m) for m in walks), which you haven't learned, and so did the other answers to the same message. To trust a program you get in one piece, you have to check all of it at once, and when it prints a wrong total, the mistake could be in any line.

So programmers build a program in small steps: one piece, run it and check it, then the next. Before the first piece comes a plan: the steps in order, each small enough to write and check on its own. A plan in words takes a minute to read, and while there's no code yet, it's easy to change.

An assistant writes good plans, but it likes to put code into them. Say that you don't want any code yet. Then ask for the code of one step, and only that one.

Mia wants a report of her week: how many walks, how many minutes in all, what she earned and the longest walk. The program in the editor has walk_price(), which charges £6 for every started half hour, this week's walks, and print_report(), which prints the report's first line so far.

Instructions

  1. 1

    Attach script.py and change the message: keep what the report should show, and ask for a plan instead of the program, with the steps in order, numbered, each small enough to write and check on its own. Say that you don't want any code yet. Then send it.

  2. 2

    Now ask in the same chat for the code of one step only: the one that adds up the minutes, as a function total_minutes(walks) that returns the minutes of all the walks. Say that you want nothing for the other steps yet.

  3. 3

    Copy the function from the answer and paste it into the editor above print_report(). Then add a line at the end of print_report(): print("Time:", total_minutes(walks), "min"). Run the program. It prints Walks: 5 and Time: 190 min: check the minutes by hand.

Back

1/7

  1. 1. A Plan Before Code
  2. 2. One Piece at a Time
  3. 3. Quiz: The Piece Just Added
  4. 4. Leave the Rest as It Is
  5. 5. A New Chat
  6. 6. Quiz: A Long Chat
  7. 7. Summary
Next

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with: