MSIT

Ask for a function

The box under the question is your prompt. Write what the function should do, in your own words. Ask sends that prompt. The model writes into the editor and then answers under your question.

Ask does not check the function. Run does. If the check fails, change the prompt and ask again, or edit the code yourself.

A prompt is a specification written in sentences. The model can draft the function, but it does not know which tests this page will run unless you say what the function takes and what it returns. The more precise those two facts are, the less you have to repair.

What to put in the prompt

The exercise below needs a function named greet. It takes one string, a name, and returns a greeting built from that name. A prompt that only says "write a greeting" leaves the name, the parameter, and the punctuation open. A prompt that states all three is something the check can agree with.

  • Name the function: greet.
  • Name the parameter: one string, the person's name.
  • Say what it returns: the text Hello, joined with that name.
  • Say that it returns the string. Do not ask it only to print.

greet("Ada") returns "Hello, Ada". The comma and the space are part of the first string. "Hello," + name would return "Hello,Ada", and the check would fail. "Hello, " + name keeps the space.

This is the same idea as label on the names page: a function builds a string and returns it. The words change. The shape does not.

The Python logo, two curved shapes in blue and yellow
Ask drafts Python. Run is what checks the function.

Ask, then run

  1. Write the prompt in the box under the question. Include the function name, the parameter, and the returned text.
  2. Ask. Read the function the model wrote before you trust it.
  3. Run the check. The check calls greet("Ada") and greet("Grace").
  4. If a call fails, edit the code or tighten the prompt and ask again.

Say the function name, what it takes, and what it returns.

The model may add a print call, use an f-string, or write a slightly different greeting. Any of those can be fine Python and still fail this page. Read the return line. It must produce "Hello, " plus the name, and the function must be named greet.

The check calls greet("Ada") and expects the string Hello, Ada. Which prompt gives the model the facts that check needs?

Match each step with what it is for.

  • Ask
  • Run
  • Prompt

Read the draft before you run the check. A short prompt can still produce a function that prints instead of returning.

Ask for a function named greet that takes a name and returns Hello, a space, and that name.

Output

The starter returns an empty string. That is not a greeting. After you ask, the editor should contain a greet that returns "Hello, " joined with name. You may also type that function yourself. Run is the step that decides whether it is done.