Decoding functions with the right prompts
You’re on the Sales Data Analytics team at a city mobility rental company that offers bikes and motorbikes for short-term rentals. Pricing is dynamic: during peak hours (7–10 AM, 5–8 PM) no discounts apply; during off-peak hours, prices can drop by up to 40% based on demand and usage.
The function below computes prices and is saved in pricing.py, a file the LLM can read and use:
def calc_price(base_price, is_peak, discount_ratio):
if is_peak:
return base_price
return base_price * (1 - 0.4 * discount_ratio)
Cet exercice fait partie du cours
AI-Assisted Coding for Developers
Instructions
- Prompt the model to improve the function for better **readability and correctness**, including context about peak and off-peak pricing and the **dynamic pricing use case**, and asking to return the answer as **PEP 8–formatted Python code**.
Exercice interactif pratique
Passez de la théorie à la pratique avec l’un de nos exercices interactifs
Commencer l’exercice