Hi, I'll try and explain my problem as simple as possible. I have written a module to perform a generalised operation (as shown below).

from math import sqrt
from math import pow
from math import log10
from Numeric import *
import numpy as np
def Entropy(parameters, temperature):
    t=float(temperature/1000.0)
    return (("%0.7f" %(float(A*log(t) + B*t + C*((pow(t,2.))/2.) + D*((pow(t,3.))/3.) - (E/(2.*(pow(t,2.)))) + G)/1000)))

The operation require values and I have put these into a dictionary in the running script (This is shown as well).

import thermodynamics
oxygen={'A':31.32234, 'B':-20.23531, 'C':57.86644, 'D':-36.50624, 'E':-0.007374, 'F':-8.903471, 'G':246.7945, 'H':0.000000,'EnthalpyTempZero':8.68, 'frequency':1580.19, 'EDFT':-4.9316435}
Oshomate= oxygen['A'], oxygen['B'], oxygen['C'],oxygen['D'], oxygen['E'],oxygen['F'],oxygen['G'],oxygen['H']
print EntropyO
EnthalpyEntropyO = thermodynamics.EnthalpyEntropy(EnthalpyO,oxygen['EnthalpyTempZero'], EntropyO, temperature)

When I run the scripts, it gives an error message "NameError: global name 'A' is not defined". How can I get the module to access the dictionary values in the running script?

Recommended Answers

All 6 Replies

return (("%0.7f" %(float(A*log(t) + B*t + C*((pow(t,2.))/2.) + D*((pow(t,3.))/3.) - (E/(2.*(pow(t,2.)))) + G)/1000)))

I see A, B...et al included but what are they?
You need to define the like A = 2, B = "You"

Big sign of business immaturity!

import thermodynamics
oxygen={'A':31.32234, 'B':-20.23531, 'C':57.86644, 'D':-36.50624, 'E':-0.007374, 'F':-8.903471, 'G':246.7945, 'H':0.000000,'EnthalpyTempZero':8.68, 'frequency':1580.19, 'EDFT':-4.9316435}
Oshomate= oxygen['A'], oxygen['B'], oxygen['C'],oxygen['D'], oxygen['E'],oxygen['F'],oxygen['G'],oxygen['H']
print EntropyO
EnthalpyEntropyO = thermodynamics.EnthalpyEntropy(EnthalpyO,oxygen['EnthalpyTempZero'], EntropyO, temperature)

When I run the scripts, it gives an error message "NameError: global name 'A' is not defined". How can I get the module to access the dictionary values in the running script?

You'll need to pass the dictionary to your Entropy function in the module thermodynamics . Then within the function you simply do as you've done in the "running script":

oxygen['A']

This returns the value of the element with the key 'A' in the dictionary oxygen

You'll need to pass the dictionary to a function in the module,

but how do I pass the dictionary to the module. is there a specific command to use.

but how do I pass the dictionary to the module. is there a specific command to use.

I don't see your specific call to the thermodynamics.Entropy() function, but here are the basics for passing a variable to a function:

# First we define a function, you'll notice it has two parameters, var1 and var2
>>> def my_function(var1, var2):
...     print 'This is the first var:', var1
...     print 'This is the 2nd var:', var2
...     
# Now I'll create two arbitrary objects, a and d
>>> a = 5
>>> d = { 'a':1, 'b':2, 'c':3 }
# To pass these two objects to my_function, I place them in the parenthesis when calling it
>>> my_function(a,d)
This is the first var: 5
This is the 2nd var: {'a': 1, 'c': 3, 'b': 2}
# Now I'll redefine the function to specifically access an element in the dictionary that I passed
>>> def my_function(var1, var2):
...     print 'This is the first var:', var1
...     print 'This is the a element in var2:', var2['a']
...     
>>> my_function(a,d)
This is the first var: 5
This is the a element in var2: 1
>>>

So that's the basics. Now for your specific case, you'll either need to modify the definition of Entropy to specifically expect a dictionary for you to access, or you'll need to roll that dictionary into the parameters object that you pass to this function.

It all depends on your preference

It's best to test the module first with something like this:

from math import pow
from math import log10

def get_entropy(d, t):
    """
    where d is the element's data dictionary and t is temperature
    """
    t = t/1000.0
    ent = (d['A']*log10(t) + d['B']*t + d['C']*pow(t,2.)/2. +
           d['D']*pow(t,3.)/3. - d['E']/(2.*(pow(t,2.))) +
           d['G'])/1000
    return "%0.7f" % ent

# test the module
if __name__ == '__main__':
    oxygen = {'A':31.32234, 'B':-20.23531, 'C':57.86644,
              'D':-36.50624, 'E':-0.007374, 'F':-8.903471,
              'G':246.7945, 'H':0.000000,
              'EnthalpyTempZero':8.68,
              'frequency':1580.19, 'EDFT':-4.9316435}
    t = oxygen['EnthalpyTempZero']
    entropy = get_entropy(oxygen, t)
    print(entropy)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.