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

What Goes In, What Comes Out

In Get Code Explained, you asked about code you had. Now you'll ask for code you don't have yet. The assistant writes it in seconds; the hard part is getting code that does what you need. It doesn't know Mia's prices, your program or what you've learned, and whatever you leave out, it fills in with a guess.

The message in the chat box is how many people start. Sent like that, one answer began:

I'll write a simple, self-contained Python program that calculates dog walking prices. It supports different walk durations, multiple dogs, discounts for recurring bookings, and extra fees, and it prints an itemized quote.

Then came a program of over 200 lines, with prices nobody had told it ($20 for 30 minutes, 10% extra at weekends) and with input(), which you haven't learned: it waits for someone to type a value while the program runs. The same message gets a different answer each time, but the others were much like this one. A program here gets nothing typed in, so input() ends it at once with an error:

minutes = int(input("Walk length in minutes: "))
Walk length in minutes: 
Traceback (most recent call last):
  File "<exec>", line 1, in <module>
EOFError: EOF when reading a line

Even where input() works, that program isn't Mia's, and it doesn't fit yours. The program in the editor needs one function, walk_price, which it calls three times. A message that gets that function says:

  • what goes in: the length of a walk in minutes;
  • what comes out: the walk's price, which the function returns;
  • the name your program calls: walk_price(minutes);
  • what only you know: Mia charges £6 a half hour.

Instructions

  1. 1

    Send the message as it is and look at the answer: which prices did the AI choose, and where do the minutes come from? Then ask in the same chat for a Python function walk_price(minutes) that takes the length of a walk in minutes and returns its price, and say that Mia charges £6 a half hour. Send it.

  2. 2

    Copy the function from the answer and paste it in place of the comment at the top of the program. Keep the three lines that print, and run it: you get the prices of walks of 30, 60 and 90 minutes, Price: 6, Price: 12 and Price: 18, or 6.0, 12.0 and 18.0, which is the same. If the answer's code also prints examples of its own, their lines come first.

Back

1/6

  1. 1. What Goes In, What Comes Out
  2. 2. Examples Settle What Words Don't
  3. 3. What You Know, Where It Runs
  4. 4. The Form You Need
  5. 5. Quiz: Which Request
  6. 6. Summary
Next

Start programming for free

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

Or sign up with: