Home > Mobile >  TypeError: float() argument must be a string or a number, not 'NoneType' Trying to take in
TypeError: float() argument must be a string or a number, not 'NoneType' Trying to take in

Time:09-26

So essentially I'm trying to make a combustion analysis solver where it takes in a mass (with the likelihood of there being decimals) of water and carbon dioxide (CO2) and then also the mass of the CxHyOz to find the empirical formula for such a problem. But it is giving me an error on my input line. It's been many years since I've written in python but I figured it would be faster this way because of the minimal syntax required lol. one small project has now taken up many hours.

The error I'm getting...

>>> 
= RESTART: C:/Users/Minec/OneDrive/Documents/Python Files/CombustionAnalysis.py
What is the mass of the CO2? 
Traceback (most recent call last):
  File "C:/Users/Minec/OneDrive/Documents/Python Files/CombustionAnalysis.py", line 9, in <module>
    massCarbonDioxide = float(print("What is the mass of the CO2? "))
TypeError: float() argument must be a string or a number, not 'NoneType'
>>> 

my code:

##Constants
waterMM = 18.0158
carbonDioxideMM = 44.0115
cMM = 12.010
hMM = 1.0078
oMM = 15.999

##variable inputs
massCarbonDioxide = float(print("What is the mass of the CO2? "))
massWater = float(print("What is the mass of the water? "))
totMass = float(print("What is the total mass of the compound X? "))

#calculating moles of CO2 and water
molesCarbonDioxide = massCarbonDioxide / carbonDioxdideMM
molesWater = massWater / waterMM

#calculating moles of Hydrogen and Carbon
molesH = 2 * molesWater
molesC = molesCarbonDioxide

#calulating mass of Hydrogen Carbon and Oxygen
massH = molesH * hMM
massC = molesC * cMM
massO = (totMass - massC) - massH

#dealing with oxygen
oxygenPresent = false
molesO = 0.0000000000000000000
if(massO > 0):
    molesO = massO / oMM
    oxygenPresent = true

#finding the minimum moles
minMole = 0.000000000000000000
if(oxygenPresent):
    if(molesH < molesC):
        if(molesH < molesO):
            minMole = molesH
        else:
            minMole = molesO
    else:
        if(molesC < molesO):
            minMoles = molesC
        else:
            minMoles = molesO
else:
    if(molesH < molesC):
        minMoles = molesH
    else:
        minMoles = molesC

#calculating mole ratio
molesH = molesH / minMoles
molesC = molesC / minMoles
if(oxygenPresent):
    molesO = molesO / minMoles

i = 1
if(oxygenPresent):
    if(molesH == 1):
        go = true
        while(go):
            if((((molesC * i) % 1) > 85) or (((molesC * i) % 1) < 15)):
                molesC = molesC * i
                molesO = molesO * i
                molesC = round(molesC,0)
                molesH = molesH * i
                if((((molesO * 1) % 1) > 85) or (((molesO * 1) % 1) < 15)):
                    molesO = round(molesO,0)
                else:
                    gogo = true
                    ii = 1
                    while(gogo):
                        if((((molesO * ii) % 1) > 85) or (((molesO * ii) % 1) < 15)):
                            molesO = molesO * ii
                            molesO = round(molesO,0)
                            molesH = molesH * ii
                            molesC = molesC * ii
                            gogo = false
                        else:
                            ii = ii   1
                go = false
            else:
                i = i   1
    elif(molesC == 1):
        go = true
        while(go):
            if((((molesH * i) % 1) > 85) or (((molesH * i) % 1) < 15)):
                molesH = molesH * i
                molesO = molesO * i
                molesH = round(molesH,0)
                molesC = molesC * i
                if((((molesO * 1) % 1) > 85) or (((molesO * 1) % 1) < 15)):
                    molesO = round(molesO,0)
                else:
                    gogo = true
                    ii = 1
                    while(gogo):
                        if((((molesO * ii) % 1) > 85) or (((molesO * ii) % 1) < 15)):
                            molesO = molesO * ii
                            molesO = round(molesO,0)
                            molesH = molesH * ii
                            molesC = molesC * ii
                            gogo = false
                        else:
                            ii = ii   1
                go = false
            else:
                i = i   1
    else:
        go = true
        while(go):
            if((((molesC * i) % 1) > 85) or (((molesC * i) % 1) < 15)):
                molesC = molesC * i
                molesH = molesH * i
                molesC = round(molesC,0)
                molesO = molesO * i
                if((((molesH * 1) % 1) > 85) or (((molesH * 1) % 1) < 15)):
                    molesH = round(molesH,0)
                else:
                    gogo = true
                    ii = 1
                    while(gogo):
                        if((((molesH * ii) % 1) > 85) or (((molesH * ii) % 1) < 15)):
                            molesH = molesH * ii
                            molesH = round(molesH,0)
                            molesO = molesO * ii
                            molesC = molesC * ii
                            gogo = false
                        else:
                            ii = ii   1
                go = false
            else:
                i = i   1
else:
    if(molesH == 1):
        go = true
        while(go):
            if((((molesC * i) % 1) > 85) or (((molesC * i) % 1) < 15)):
                molesC = molesC * i
                molesC = round(molesC,0)
                molesH = molesH * i
                go = false
            else:
                i = i   1
    else:
        go = true
        while(go):
            if((((molesH * i) % 1) > 85) or (((molesH * i) % 1) < 15)):
                molesH = molesH * i
                molesH = round(molesH,0)
                molesC = molesC * i
                go = false
            else:
                i = i   1


#printing out the empirical formula
if(oxygenPresent):
    print("C",molesC,"H",molesH,"O",molesO)
else:
    print("C",molesC,"H",molesH)
  


CodePudding user response:

I am very dumb the input lines under secition ##variable inputs needed to read:

##variable inputs
massCarbonDioxide = float(input("What is the mass of the CO2? "))
massWater = float(input("What is the mass of the water? "))
totMass = float(input("What is the total mass of the compound X? "))

CodePudding user response:

Note that the Python print function does not return anything. Instead, it simply outputs to the console. That is why Python is complaining about trying to convert a none type to a float.

You need an input, however, and that can be done with the input function like so:

massCarbonDioxide = float(input("What is the mass of the CO2? "))

Just to note, your booleans also should be capitalized. Rather than false, it should be False!

  • Related