-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello, -tutor. I wrote this script in chemistry class, which gets the
user's input for various parts of the PV=nRT ideal gas law, then
calculates the missing variable, which you indicate by putting in an 'x'.

I've tried to follow 'best practices' per se, and I'm looking for
feedback on if I've done anything that's a Python or programming no-no.

I've attached the script, and it's online with highlighting here:
http://www.adamgomaa.com/docuwiki/doku.php?id=pvnrt2.py
Incidentally, that's a wiki, so you can edit it there if you like.

Thanks for your time.

Full disclosure: I am taking an Intro. to Programming course in which
we use Python, but this is not my homework- we're actually moving much
more slowly than I would like :/
And, this isn't work for the chemistry class either. I do use this
script in class, but not on graded assignments/tests during which we
can't use computers anyway...
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD4DBQFFPXOrOvYxdjZi+ggRAjl3AJUSlJNb3g9vqIJ9TDht2Ga/e7OQAJ9+jinx
ddN7X9VJghNwtwROhlA78A==
=MP74
-----END PGP SIGNATURE-----

#!/usr/bin/python
#Second try at a PV=NRT script. I have all the stuff I need in
# the other script; this one is to make it actually work 
# and to make debugging easier. The other one marginally works but
# I have no idea how. So, I wrote this one.

# Started and pretty much finished October 23d, 2006.

"""Prompt for parts of the Ideal Gas Equation, return wanted
variable, indicated by an 'x'

Nothing particularly fancy about this. 

It also works, so far as I can tell. 
"""


def init(): 
    """Initialize needed variables."""
    
    # I'm sure there's a built-in fuction to do what I'm
    # doing right here, but I don't know it off-hand and this works anyway
    global numbers
    numbers='0123456789 '#Needed to strip out of user input for calcs/disambig
    
    global letters
    letters='abcdefghijklmnopqrstuvwxyz ' #Needed to strip out from input
    
    global R
    R=8.314472 #in kPa
    #This is R for kilopascals, Liters, and kelvin. I used kPA
    #because it was the one I knew offhand when writing the script. 
    #Everything will be converted to these units before going into PV=nRT.


def getInput():
    """Return the User's input."""
    input_pressure=raw_input("What is the Pressure? ")
    input_volume=raw_input("What is the Volume? ")
    input_temperature=raw_input("What is the temperature?")
    input_moles=raw_input("How Many Moles?")
    #Pretty straightforward. No error checking.
    
    return input_pressure,input_volume,input_temperature,input_moles


def parsingLogic(p,v,t,m):
    """Parse the user's input to find the units and wanted variable. 
    If units are not the ones we need, convert them.  """
    temp_list=[p,v,t,m] 
    if 'x' not in temp_list: #error check. We need an X to calculate for. 
        print "################   No variable found!     ##################"
        return False # Causes an error, hence the noticable string above
    
    else: #make a variable called unknown_variable. 
        if 'x'==p:
            unknown_variable='p'
        elif 'x'==v:
            unknown_variable='v'
        elif 'x'==t:
            unknown_variable='t'
        elif 'x' ==m:
            unknown_variable='m'
        #return unknown_variable at the end. 
        
    
    for each in temp_list: # sheesh, hope it works.
        if each != 'x':# skip if it's the unknown variable - no need to convert
            # This next if block captures non-default units and converts
            # them to what we want before we plug in PV=nRT.
            if each == p: #Catch units for pressure
                _p=p.strip(numbers) #make temp _p variable
                _p=_p.lower()
                if _p=="atm" or _p=='a': 
                    p=(float(p.strip(letters))*101.325) #convert to kPa
                elif _p=="torr" or _p=="tor" or _p=="mmhg":
                    p=(float(p.strip(letters))*101.325/760.) #convert to kPa
                else:
                    p=p.strip(letters) #otherwise, assume kPa. 
                    
                
            
            elif each == v: 
                _v=v.strip(numbers)
                _v=_v.lower()
                if _v=='ml':
                    v=(float(v.strip(letters))/1000.) #convert mL to L
                elif _v=='m' or _v=='m3':
                    v=(float(v.strip(letters))*1000.) #convert m^3 to L
                else:
                    v=float(v.strip(letters)) #Otherwise, assume  L. 
                    
                
            
            elif each==t:
                _t=t.strip(numbers)
                _t=_t.lower()
                if _t=='c':
                    t=float(t.strip(letters))+273.15 #convert C to k
                elif _t=="f": #convert F to K
                    t=(float(t.strip(letters))-32.)*(5./9.)+273.15
                else:
                    t=t.strip(letters) #otherwise, assume K
            # ENDIF
    #end For loop.    return unknown_variable,p,v,t,m #return the stripped, numeric (but still
                # string) p,v,t, and m. 


def calculations(unknown_variable,pres,vol,temp,moles): #PV=nRT
    #Sorry for all the floats, but they carry as strings from the raw_input
    def calcPress(vol,temp,moles): #P=nRT/V
        return float(float(moles)*R*float(temp)/float(vol))
    
    def calcVol(pres,temp,moles): #V=nRT/P
        return float(float(moles)*R*float(temp)/float(pres))
    
    def calcTemp(pres,vol,moles): #T=PV/nR
        return float(float(pres)*float(vol)/(float(moles)*R))
    
    def calcMoles(pres,vol,temp): #n=PV/RT
        return float(float(pres)*float(vol)/(R*float(temp)))
    
    
    if unknown_variable=='p':
        return calcPress(vol,temp,moles)
    elif unknown_variable=='v':
        return calcVol(pres,temp,moles)
    elif unknown_variable=='t':
        return calcTemp(pres,vol,moles)
    elif unknown_variable=='m':
        return calcMoles(pres,vol,temp)
    #ENDIF


def output(passedVar):
    print passedVar


def main():
    init()
    Pressure,Volume,Temperature,Moles=getInput()
    Wanted,Pressure,Volume,Temperature,Moles=parsingLogic(Pressure,Volume,
Temperature,Moles) #global variable issues here... (works now)
    Solution=calculations(Wanted,Pressure,Volume,Temperature,Moles)
    output(Solution)


###########      MAIN       #########
main()
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to