IniziaInizia gratis

Modifying code with multi-step prompts

You are a home improvement contractor specializing in flooring installations. You need to develop a Python function that calculates the area and perimeter of a rectangular floor in a room to help you determine the amount of flooring material required for the project.

You are given a string named function, which contains a starter function that calculates the area of a rectangular floor given its width and length. Using a multi-step prompt, you need to prompt the language model to modify this function to return the perimeter of the rectangle as well, and to test if the inputs (floor dimensions) are positive, and if not, display appropriate error messages.

The OpenAI package, the function string, and the get_response() function have been pre-loaded for you.

Questo esercizio fa parte del corso

Prompt Engineering with the OpenAI API

Visualizza il corso

Istruzioni dell'esercizio

  • Craft a multi-step prompt that asks the model to modify the function according to the specified requirements: test if the inputs to the functions are positive, and if not, display appropriate error messages, otherwise return the area and perimeter of the rectangle.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

client = OpenAI(api_key="")

function = """def calculate_area_rectangular_floor(width, length):
					return width*length"""

# Craft a multi-step prompt that asks the model to adjust the function
prompt = ____

response = get_response(prompt)
print(response)
Modifica ed esegui il codice