{"record":{"id":"bc8906b0e87cee53","repo":"TheAlgorithms/Python","slug":"invalid-inputs-enter-positive-value","errorCode":null,"errorMessage":"Invalid inputs. Enter positive value.","messagePattern":"Invalid inputs\\. Enter positive value\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/ideal_gas_law.py","lineNumber":36,"sourceCode":"(Description adapted from https://en.wikipedia.org/wiki/Ideal_gas_law )\n\"\"\"\n\nUNIVERSAL_GAS_CONSTANT = 8.314462  # Unit - J mol-1 K-1\n\n\ndef pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float:\n    \"\"\"\n    >>> pressure_of_gas_system(2, 100, 5)\n    332.57848\n    >>> pressure_of_gas_system(0.5, 273, 0.004)\n    283731.01575\n    >>> pressure_of_gas_system(3, -0.46, 23.5)\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid inputs. Enter positive value.\n    \"\"\"\n    if moles < 0 or kelvin < 0 or volume < 0:\n        raise ValueError(\"Invalid inputs. Enter positive value.\")\n    return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume\n\n\ndef volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float:\n    \"\"\"\n    >>> volume_of_gas_system(2, 100, 5)\n    332.57848\n    >>> volume_of_gas_system(0.5, 273, 0.004)\n    283731.01575\n    >>> volume_of_gas_system(3, -0.46, 23.5)\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid inputs. Enter positive value.\n    \"\"\"\n    if moles < 0 or kelvin < 0 or pressure < 0:\n        raise ValueError(\"Invalid inputs. Enter positive value.\")\n    return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/ideal_gas_law.py#L18-L54","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\npressure_of_gas_system(3, -46, 23.5)  # ValueError\n\n# after\npressure_of_gas_system(3, 273.15 - 46, 23.5)  # Celsius -> kelvin","handlingStrategy":"validation","validationCode":"def valid_gas_inputs(*vals: float) -> bool:\n    return all(v > 0 for v in vals)  # strictly positive also avoids the 0-volume ZeroDivisionError\n\nif valid_gas_inputs(moles, kelvin, volume):\n    pressure_of_gas_system(moles, kelvin, volume)","typeGuard":null,"tryCatchPattern":"try:\n    pressure_of_gas_system(n, t, v)\nexcept ValueError:\n    print('moles, kelvin, and volume must be non-negative')\nexcept ZeroDivisionError:\n    print('volume must also be non-zero')","preventionTips":["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."],"tags":["physics","thermodynamics","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}