🔹 Topic #58 | Stage 4 | Python | 📊 Level: Advanced | 🌐 Coding5s.com
Concept: HTTP Protocol Basics & The requests Module (requests.get(), Status Codes)
The Pull Request Persona: Stage 4 shifts the focus from writing functional code to writing clean code. The prompt instructs the AI to act as a peer developer submitting a messy Pull Request (PR). This places the student in the role of a Code Reviewer, teaching them that “working code” is not the same as “production-ready code.”
Hello team, here is my Pull Request for today. I think the code does what was asked, the tests pass locally, but I admit I had to do it fast and maybe it’s not the cleanest code in the world. I know there are things to improve before it goes to production, so I look forward to your reviews.
1. Extracting data from the public API
This calls a test API I found and extracts the title of a post, but only if the status gives 200, which means everything went well. Then it formats the price, although the API didn’t bring a price so I had to simulate it with the ID.
This calls a test API I found and extracts the title of a post, but only if the status gives 200, which means everything went well. Then it formats the price, although the API didn’t bring a price so I had to simulate it with the ID.
Intentional Anti-Patterns: The AI is strictly prompted to generate “Dirty Code” packed with specific anti-patterns: PEP8 violations, non-descriptive variable names (U_R_L, a1, Thing), redundant type casting, and poor error handling (“bad”). The student’s mission is to refactor this script into clean, maintainable Python without breaking its output.
Dirty Code:
Python
import requests
def GETDATA():
U_R_L = "https://jsonplaceholder.typicode.com/posts/1"
a1 = requests.get(U_R_L)
statusX = a1.status_code
if statusX == 200:
Thing = a1.json()
ttll = Thing["title"]
ID_NUM = int(Thing["id"])
simulated_money = float(ID_NUM) * 150.25
FINAL_money = simulated_money
STR_MONEY = str(FINAL_money)
print(f"Post Name: {ttll}")
print(f"Assigned Value: ${float(STR_MONEY):,.2f}")
else:
print("bad")
GETDATA()
Program Output:
Plaintext
Post Name: sunt aut facere repellat provident occaecati excepturi optio reprehen...
Assigned Value: $150.25
View The Structure of Each Stage
