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

Tests First, Then the Code

In Say What the Code Should Do, examples settled what words couldn't. Tests are examples that run, so write them before you ask for the code. Then they do two jobs: they tell the AI exactly what the function has to return, and once you paste in its answer, they check it.

Max is still a puppy, and his walks are short, so Mia is thinking of a lower price for him: £4 for every started quarter hour. A walk of 15 minutes would cost £4, and one of 16 minutes £8, because the second quarter hour has started. The function puppy_price() in the editor is a stand-in: it returns 0 for now, so that the program runs before the real code is there. Your tests print False at first. Tests fail until the code is written, and when they print True, you know it was the new code that made them pass.

This time, send the tests without the rule. The AI then has to work the rule out from them, and if it gets the function right, your tests say what you mean. Choose them so that only Mia's rule fits them. Three tests can fit another rule too: “£4 up to 15 minutes, £8 up to 30 and £12 for any longer walk” passes tests for 15, 16 and 40 minutes, and it isn't Mia's rule. Your tests sit at the end of the first quarter hour and one minute past each quarter hour after it, where her rule adds £4 every time.

Instructions

  1. 1

    Under the function, write four tests: print(puppy_price(15) == 4), and three more in the same way for walks of 16, 31 and 46 minutes, with the prices you work out. Run the program: the tests print False, since the function isn't written yet.

  2. 2

    Attach script.py and ask for the function puppy_price() that makes your tests print True, without saying the rule. Then send it.

  3. 3

    Copy the function from the answer and paste it in place of the stand-in, the two lines of puppy_price() that return 0. Run the program: the tests print True four times, and the last line is Price: 20.

Back

2/7

  1. 1. An Example That Runs
  2. 2. Tests First, Then the Code
  3. 3. Check the Tests Too
  4. 4. Quiz: The Function or the Test?
  5. 5. Tests with assert
  6. 6. Quiz: Which Line Is Wrong?
  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: