TheAlgorithms/Python · error · ValueError
Purchase value cannot be less than zero
Error message
Purchase value cannot be less than zero
What it means
Raised by straight_line_depreciation() when purchase_value is numeric but negative (ValueError). A negative purchase price makes depreciable_cost negative and produces a nonsense schedule of negative expenses, so the function rejects it after type checks. Zero is allowed.
Source
Thrown at financial/straight_line_depreciation.py:68
[30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0]
>>> straight_line_depreciation(1, 4985, 100)
[4885.0]
"""
if not isinstance(useful_years, int):
raise TypeError("Useful years must be an integer")
if useful_years < 1:
raise ValueError("Useful years cannot be less than 1")
if not isinstance(purchase_value, (float, int)):
raise TypeError("Purchase value must be numeric")
if not isinstance(residual_value, (float, int)):
raise TypeError("Residual value must be numeric")
if purchase_value < 0.0:
raise ValueError("Purchase value cannot be less than zero")
if purchase_value < residual_value:
raise ValueError("Purchase value cannot be less than residual value")
# Calculate annual depreciation expense
depreciable_cost = purchase_value - residual_value
annual_depreciation_expense = depreciable_cost / useful_years
# List of annual depreciation expenses
list_of_depreciation_expenses = []
accumulated_depreciation_expense = 0.0
for period in range(useful_years):
if period != useful_years - 1:
accumulated_depreciation_expense += annual_depreciation_expense
list_of_depreciation_expenses.append(annual_depreciation_expense)
else:
depreciation_expense_in_end_year = (
depreciable_cost - accumulated_depreciation_expenseView on GitHub (pinned to f5988cc097)
Solutions
- Pass purchase_value >= 0.
- Investigate the source of the negative basis — usually an upstream double-entry error worth surfacing, not silently fixing.
- If reversals are legitimate in your pipeline, handle them before depreciation, not through this API.
Example fix
# before
straight_line_depreciation(5, net_cost_basis, 50.0) # -1250.0 after reversals
# after
if net_cost_basis < 0:
raise ValueError(f'negative cost basis from reversals: {net_cost_basis}')
straight_line_depreciation(5, net_cost_basis, 50.0) Defensive patterns
Strategy: validation
Validate before calling
if purchase_value < 0:
raise ValueError(f'purchase value cannot be negative: {purchase_value}')
straight_line_depreciation(5, purchase_value, residual) Try / catch
try:
sched = straight_line_depreciation(y, p, r)
except ValueError as exc:
if 'less than zero' in str(exc):
flag_for_review(p)
raise
raise Prevention
- Zero purchase is legal (fully-residual asset); only negatives raise.
- Audit negative monetary inputs at import time — they usually indicate data corruption.
When it happens
Trigger: Calling straight_line_depreciation(5, -1250.0, 50.0) or passing a credit-note-adjusted cost basis that went below zero.
Common situations: Accounting systems storing reversals/refunds as negatives, currency-adjusted bases that underflow to negative, or sign-convention mistakes when importing GL data.
Related errors
- number_of_years must be > 0
- nominal_annual_percentage_rate must be >= 0
- Discount rate cannot be negative
- Cash flows list cannot be empty
- Window size must be a positive integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/10659e1197025752.
Report an issue: GitHub.