{"record":{"id":"a4c614376254fc69","repo":"TheAlgorithms/Python","slug":"molar-mass-should-be-greater-than-0-kg-mol","errorCode":null,"errorMessage":"Molar mass should be greater than 0 kg/mol","messagePattern":"Molar mass should be greater than 0 kg/mol","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"physics/speeds_of_gas_molecules.py","lineNumber":78,"sourceCode":"\n    >>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K\n    454.3488755062257\n    >>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K\n    445.5257273433045\n    >>> avg_speed_of_molecule(-273, 0.028) # invalid temperature\n    Traceback (most recent call last):\n        ...\n    Exception: Absolute temperature cannot be less than 0 K\n    >>> avg_speed_of_molecule(273, 0) # invalid molar mass\n    Traceback (most recent call last):\n        ...\n    Exception: Molar mass should be greater than 0 kg/mol\n    \"\"\"\n\n    if temperature < 0:\n        raise Exception(\"Absolute temperature cannot be less than 0 K\")\n    if molar_mass <= 0:\n        raise Exception(\"Molar mass should be greater than 0 kg/mol\")\n    return (8 * R * temperature / (pi * molar_mass)) ** 0.5\n\n\ndef mps_speed_of_molecule(temperature: float, molar_mass: float) -> float:\n    \"\"\"\n    Takes the temperature (in K) and molar mass (in kg/mol) of a gas\n    and returns the most probable speed of a molecule in the gas (in m/s).\n\n    Examples:\n\n    >>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K\n    402.65620702280023\n    >>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K\n    394.8368955535605\n    >>> mps_speed_of_molecule(-273, 0.028) # invalid temperature\n    Traceback (most recent call last):\n        ...\n    Exception: Absolute temperature cannot be less than 0 K","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/speeds_of_gas_molecules.py#L60-L96","documentation":"Raised by physics/speeds_of_gas_molecules.py:avg_speed_of_molecule when molar_mass <= 0. Molar_mass appears in the denominator of sqrt(8RT/(pi*M)); zero divides by zero and a negative value yields a complex speed. Like the sibling checks in this file it is a bare Exception, not ValueError.","triggerScenarios":"Calling avg_speed_of_molecule(273, 0) or with a negative second argument, e.g. avg_speed_of_molecule(273, -0.028).","commonSituations":"Passing molar mass in g/mol (28) instead of kg/mol (0.028) usually gives a wrong but non-raising answer, while passing 0 happens when a lookup table misses the gas; using an atomic mass for a diatomic gas without multiplying by 2 and hitting an uninitialized 0 default; parsing failures that yield 0 for unknown species strings.","solutions":["Pass molar mass in kg/mol - N2 is 0.028, O2 is 0.032, CO2 is 0.044; divide g/mol values by 1000.","Validate molar_mass > 0 before the call, especially when it comes from a species lookup that can return 0/None.","Because this is a bare Exception, catch Exception (and check the message) or pre-validate rather than expecting ValueError.","Guard gas-name-to-molar-mass mappings with a KeyError/None check so unknown species never reach this function as 0."],"exampleFix":"# before\navg_speed_of_molecule(273, 0)  # missing lookup -> 0\n\n# after\nMOLAR_MASS_KG = {'N2': 0.028, 'O2': 0.032, 'CO2': 0.044}\nm = MOLAR_MASS_KG[gas]  # KeyError surfaces the real problem\nif m <= 0:\n    raise ValueError(f'bad molar mass for {gas!r}')\navg_speed_of_molecule(273, m)","handlingStrategy":"validation","validationCode":"if molar_mass is None or molar_mass <= 0:\n    raise ValueError(f'molar_mass must be > 0 kg/mol, got {molar_mass!r}')\nv = avg_speed_of_molecule(temperature, molar_mass)","typeGuard":"def is_valid_molar_mass_kg(m: float) -> bool:\n    return isinstance(m, (int, float)) and 0 < m < 1.0  # kg/mol values are < 1","tryCatchPattern":"try:\n    avg_speed_of_molecule(t, m)\nexcept Exception as e:\n    if 'Molar mass' in str(e):\n        ...","preventionTips":["Keep molar masses in kg/mol (0.028 for N2); a value > 1 usually means g/mol slipped in.","Make species lookups raise KeyError instead of returning 0.","Catch Exception (not ValueError) around this module's calls."],"tags":["physics","gas","units","molar-mass","bare-exception"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}