It's pretty simple actually. The molecular weight of a compound is the sum of the atomic weights of all its atoms. The same numbers apply to a single atom as for a mole of that atom, but at the single-molecule level the unit of measure is the atomic mass unit or amu (aka the dalton or Da). The number of grams per mole of an element and the number of daltons that a single atom of the element weighs are intrinsically linked due to the definition of what a mole of something is (the amount of a pure chemical substance that weighs the same amount in grams as the mass of one atom or molecule of the substance in daltons).
So, methane (CH4), for instance, has a molecular weight of 1.0079 x 4 + 12.011 = 16.0426 Da. A mole of this gas would weigh 16.0426 grams (which is - interesting science fact - lighter by almost half than that of N2 or O2 gases making up most of the atmosphere, so a balloon of it will float). So, your Python script would simply prompt for the number of hydrogens (accepting integer values only), carbons (ditto) and oxygens (ditto), and plug them into the expression:
total_mass = num_hyd * 1.0079 + num_carb * 12.011 + num_oxy * 15.9994
... before returning
total_mass as the answer.
Theoretically, the energy contained as enthalpy in the bonds between atoms contributes to a molecule's mass by general relativity and the first law of thermodynamics. In practice, the amount of energy inherent in even the least stable bonds (which produce the most net energy when they decompose to form more stable ones) is a rounding error when you run it through E=mc2, and so chemists don't bother. Nuclear physicists, who actually change the atoms themselves, do care, because the equation works in reverse for them; they get lots of energy from very small changes in the atomic masses of the reactants vs the products.