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

Next comes the discount. For a customer's month, Mia has a rule of its own: in a month of 10 walks or more, the customer pays a tenth less, rounded down to whole pounds. Mrs Carter had 10 walks in May for £96. A tenth of that is £9.60, so her discount is £9.

A rule in words leaves things open. Asked for discount(count, total) with only this rule, one answer returned total / 10, which for May is 9.6 instead of 9: it didn't round down. The same message gets a different answer each time, but the others were much like it. So, as in Tests Come First, write the tests before you ask for the code. A test is an example that runs: it calls the function with numbers you've worked out by hand and prints True if the function gets them right. Attached to your message, the tests tell the AI exactly what the function has to do: whether 10 walks count, and how it rounds. Run with its function, they tell you whether it does.

So that the tests can run before the code is there, the editor now has a stand-in discount() above the line that prints the heading, which returns 0 for now. Your tests print False, True and False at first: only the second one passes, since 9 walks really get no discount. Tests fail until the code is written, and when they all print True, you know it was the new code that made them pass.

Instructions

  1. 1

    At the end of the program, write three tests, each on a line of its own: print(discount(10, 96) == 9), print(discount(9, 96) == 0) and print(discount(12, 150) == 15). A month of 9 walks gets no discount, and 12 walks for £150 get £15 off. Run the program: the tests print False, True and False.

  2. 2

    Attach script.py and ask for a function discount(count, total) to replace the one that returns 0, so that your tests pass: count is the number of walks in the month, and total is the month's total. Then send it.

  3. 3

    Copy the function from the answer and paste it in place of the two lines of discount() that return 0. Run the program: under the weeks, your tests print True, True and True.

Back

2/6

  1. 1. The Invoice
  2. 2. Tests First
  3. 3. One Step at a Time
  4. 4. What the Test Shows
  5. 5. Feedback on Your Program
  6. 6. Congratulations!
Next

Start programming for free

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

Or sign up with: