TheAlgorithms/Python · error · ValueError
Invalid inputs. Enter positive value.
Error message
Invalid inputs. Enter positive value.
What it means
Raised by pressure_of_gas_system(moles, kelvin, volume) when moles, kelvin, or volume is negative, since amount of substance, absolute temperature, and volume are physically non-negative. It computes P = nRT/V. Note zero volume is NOT caught here and will raise ZeroDivisionError instead.
Source
Thrown at physics/ideal_gas_law.py:36
(Description adapted from https://en.wikipedia.org/wiki/Ideal_gas_law )
"""
UNIVERSAL_GAS_CONSTANT = 8.314462 # Unit - J mol-1 K-1
def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float:
"""
>>> pressure_of_gas_system(2, 100, 5)
332.57848
>>> pressure_of_gas_system(0.5, 273, 0.004)
283731.01575
>>> pressure_of_gas_system(3, -0.46, 23.5)
Traceback (most recent call last):
...
ValueError: Invalid inputs. Enter positive value.
"""
if moles < 0 or kelvin < 0 or volume < 0:
raise ValueError("Invalid inputs. Enter positive value.")
return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume
def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float:
"""
>>> volume_of_gas_system(2, 100, 5)
332.57848
>>> volume_of_gas_system(0.5, 273, 0.004)
283731.01575
>>> volume_of_gas_system(3, -0.46, 23.5)
Traceback (most recent call last):
...
ValueError: Invalid inputs. Enter positive value.
"""
if moles < 0 or kelvin < 0 or pressure < 0:
raise ValueError("Invalid inputs. Enter positive value.")
return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure
View on GitHub (pinned to f5988cc097)
Solutions
- Convert temperatures to kelvin (add 273.15 to Celsius) before calling.
- Sanitize inputs: reject or clamp negative moles/kelvin/volume before the call.
- Also guard volume == 0 yourself, since the library only checks < 0 and zero causes ZeroDivisionError.
Example fix
# before pressure_of_gas_system(3, -46, 23.5) # ValueError # after pressure_of_gas_system(3, 273.15 - 46, 23.5) # Celsius -> kelvin
Defensive patterns
Strategy: validation
Validate before calling
def valid_gas_inputs(*vals: float) -> bool:
return all(v > 0 for v in vals) # strictly positive also avoids the 0-volume ZeroDivisionError
if valid_gas_inputs(moles, kelvin, volume):
pressure_of_gas_system(moles, kelvin, volume) Try / catch
try:
pressure_of_gas_system(n, t, v)
except ValueError:
print('moles, kelvin, and volume must be non-negative')
except ZeroDivisionError:
print('volume must also be non-zero') Prevention
- Always convert Celsius to kelvin (+273.15) at your boundary.
- Require strictly positive values yourself; the library allows 0 and then may divide by zero.
- Tag parameters with units in variable names (kelvin, volume_m3) to catch mixups.
When it happens
Trigger: pressure_of_gas_system(3, -0.46, 23.5) from the doctest; any call with a negative temperature in Celsius-style values (e.g. -20) fed directly as kelvin.
Common situations: Feeding Celsius temperatures into a parameter that expects kelvin; negative volumes from upstream subtraction bugs; unit-conversion mistakes between m^3 and liters producing sign or scale errors.
Related errors
- Expected a_coeffs to have {self.order + 1} elements for {sel
- n must not be negative
- Depth cannot be less than 0
- Invalid velocity. Should be a positive number.
- All input parameters must be positive
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/bc8906b0e87cee53.
Report an issue: GitHub.