{"record":{"id":"84ffd06505721465","repo":"TheAlgorithms/Python","slug":"molar-mass-cannot-be-less-than-or-equal-to-0-kg-mo","errorCode":null,"errorMessage":"Molar mass cannot be less than or equal to 0 kg/mol","messagePattern":"Molar mass cannot be less than or equal to 0 kg/mol","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"physics/rms_speed_of_molecule.py","lineNumber":36,"sourceCode":"way, which means that the average velocity for a collection of gas particles equals\nzero; as this value is unhelpful, the average of velocities can be determined using an\nalternative method.\n\"\"\"\n\nUNIVERSAL_GAS_CONSTANT = 8.3144598\n\n\ndef rms_speed_of_molecule(temperature: float, molar_mass: float) -> float:\n    \"\"\"\n    >>> rms_speed_of_molecule(100, 2)\n    35.315279554323226\n    >>> rms_speed_of_molecule(273, 12)\n    23.821458421977443\n    \"\"\"\n    if temperature < 0:\n        raise Exception(\"Temperature cannot be less than 0 K\")\n    if molar_mass <= 0:\n        raise Exception(\"Molar mass cannot be less than or equal to 0 kg/mol\")\n    else:\n        return (3 * UNIVERSAL_GAS_CONSTANT * temperature / molar_mass) ** 0.5\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    # run doctest\n    doctest.testmod()\n\n    # example\n    temperature = 300\n    molar_mass = 28\n    vrms = rms_speed_of_molecule(temperature, molar_mass)\n    print(f\"Vrms of Nitrogen gas at 300 K is {vrms} m/s\")\n","sourceCodeStart":18,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/rms_speed_of_molecule.py#L18-L52","documentation":"Raised by rms_speed_of_molecule(temperature, molar_mass) when molar_mass <= 0, guarding the division and square root in v = sqrt(3*R*T/M). Zero is also rejected (unlike temperature, where 0 K is fine). Like its sibling check it raises a bare built-in Exception, so 'except ValueError' will not catch it.","triggerScenarios":"rms_speed_of_molecule(300, 0) — zero molar mass; rms_speed_of_molecule(300, -0.032) — negative molar mass, e.g. from a bad lookup or a sign typo; molar-mass tables keyed by gas name returning 0 for an unknown gas.","commonSituations":"Unknown gas falling through a dict lookup to a 0 default; unit mix-ups between g/mol and kg/mol that push values through lossy conversions; per-particle mass passed instead of molar mass.","solutions":["Pass molar mass in kg/mol, strictly positive (e.g. 0.028 for N2, 0.002 for H2 as in the doctests)","Make gas-property lookups raise KeyError for unknown gases instead of defaulting to 0","Pre-check molar_mass > 0 and catch Exception (not ValueError) around this call"],"exampleFix":"# before\nMOLAR_MASS = {'co2': 0.044, 'n2': 0.028}\nrms_speed_of_molecule(300, MOLAR_MASS.get('o2', 0))  # unknown gas -> 0\n# Exception: Molar mass cannot be less than or equal to 0 kg/mol\n\n# after\nMOLAR_MASS = {'co2': 0.044, 'n2': 0.028, 'o2': 0.032}\nrms_speed_of_molecule(300, MOLAR_MASS['o2'])","handlingStrategy":"try-catch","validationCode":"if molar_mass <= 0:\n    raise ValueError(f\"molar mass must be > 0 kg/mol, got {molar_mass}\")\nrms_speed_of_molecule(temperature, molar_mass)","typeGuard":null,"tryCatchPattern":"try:\n    v = rms_speed_of_molecule(temperature, molar_mass)\nexcept Exception as e:  # bare Exception, not ValueError\n    if \"Molar mass\" in str(e):\n        raise ValueError(\"gas lookup returned invalid molar mass\") from e\n    raise","preventionTips":["Pass molar mass in kg/mol (e.g. 0.028 for N2), strictly positive — 0 is also rejected","Make gas-property dicts raise KeyError for unknown gases instead of defaulting to 0","Catch Exception here, not ValueError"],"tags":["physics","kinetic-theory","validation","exception","lookup"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}