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

Reading a Change

When an agent has done a task, read what it changed before you keep it. Agents and code editors show a change line by line: a line that starts with - was taken out, a line that starts with + was put in, and the lines around them show where. Many show them in red and green.

This is a change of the kind agents make, for the discount from What a Coding Agent Does. The agent's summary said only: “I added the discount and ran the program: it prints Total: 37.”

 def walk_price(minutes):
     half_hours = minutes // 30
-    if minutes % 30 > 0:
+    if minutes % 30 >= 15:
         half_hours = half_hours + 1
     return half_hours * 6
 
 walks = [45, 30, 75, 20]
 
 total = 0
 for minutes in walks:
     total = total + walk_price(minutes)
 
+if len(walks) > 3:
+    total = total - 5
+
 print("Walks:", len(walks))
 print("Total:", total)

The lines it put in near the end are the discount. But it also changed a line in walk_price(), which nobody asked for. Agents do that: they tidy up code that works, rename what you named, or “fix” what they take for a mistake. A change you didn't ask for isn't always wrong, but it's yours to decide on, and this one changes Mia's prices.

The program still prints Walks: 4 and Total: 37, as the agent said: with these four walks, both versions of the line give the same prices. Running the program didn't show the change; reading it did. The program in the editor is the agent's version.

Instructions

  1. 1

    Find out what the line nobody asked for does to the price: at the end of the program, add print("Price:", walk_price(40)), which prints the price of a 40-minute walk, and run it. A walk of 40 minutes has started its second half hour, so it should cost 12, but the agent's version prints Price: 6.

  2. 2

    Put the line back as it was before the change, and keep the discount. Run the program: it prints Walks: 4, Total: 37 and Price: 12.

Back

2/7

  1. 1. What a Coding Agent Does
  2. 2. Reading a Change
  3. 3. Quiz: It Says It Works
  4. 4. What Not to Send
  5. 5. Don't Run What You Can't Read
  6. 6. Quiz: Say When It Helped
  7. 7. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.