EasyPython

Replace Magic Numbers

PythonClean CodeReadabilityRefactoring

Magic numbers make code unreadable. What does 86400000 mean? What about 0.2?

# dirty
def solve(price_in_cents, age_in_ms):
    discounted = price_in_cents * (1 - 0.2)
    age_in_days = age_in_ms // 86400000
    return {'discounted': round(discounted), 'age_in_days': age_in_days}

Refactor by introducing named constants so the intent is self-documenting.
The output must remain identical.

Sample tests

Test #11000 cents with 20% discount = 800; 1 day in ms = 1 day
Input: [1000,86400000]
Output: {"discounted":800,"age_in_days":1}
Test #2Zero age → 0 days
Input: [500,0]
Output: {"discounted":400,"age_in_days":0}
Test #32 days in ms, price of 0
Input: [0,172800000]
Output: {"discounted":0,"age_in_days":2}