-1

I'm completely new to python. Now i'm using Enthought canopy (python 2.7.3). I know this question has been asked a million times before and i can imagine you all are tired of this question but it has been bugging me all day.

import Basismodel

def doAll(c):
   #from Basismodel import calcKappa
   Basismodel.setC(c)
   Basismodel.setMu()
   Basismodel.setLambda()
   Basismodel.calcKappa()
   Basismodel.calcSumofprob()
   Basismodel.calcPi()

doAll(100)

With another file Basismodel.py where all functions mentioned in here are defined. It doesn't give any error with the first 3 but it does with the last 3.

As extra information I'll give you the code for Basismodel.py as well

global c
global lamb
global pi
global kappa
global mu
global sumofkappa
global f
f=0.6 #the percentage that takes the car
c=100 #max
sumofkappa=0 #sum of all kappa
sumofpi=0 #sum of all probabilities (should be equal to 1)
pi=[]
kappa=[1.0]
mu=[0.4]
lamb=[0.1] #Lambda is de arrival rate

def setC(y):
    c=y
    #print c

def setMu():
    global mu
    for i in range (c):
        mu.append((i+2)*mu[0])
        #print mu[i]

def setLambda():

    for i in range (c):
        lamb.append(lamb[1])
        #print lamb[i]

def calcKappa():

    for i in range (c):
        if (i==0):
            kappa[0]=1.0
        else:
            kappa[i].append(kappa[i-1]*lamb[i-1]/mu[i])

def calcSumofprob():
    for i in range (c):
        global sumofkappa
        sumofkappa += kappa[i]

def calcPi():
    for i in range (c):
        pi.append(kappa[i]/sumofkappa)

def calcAveragePi():
    for i in range(c):
        global sumofpi
        sumofpi += pi[i]
    return sumofpi/c

When I execute the main it gives this error, it doesn't make any sense since all print lines are in #comment style. However I am more interested in why it can't find the attribute. Also when i set "#from .Basismodel import calcKappa" after def doAll, the error changes to an importerror where it can't import.

%run "C:/Users/Thomas/Dropbox/Thesis/Canopy files/Main.py"
100
0.4
0.8
1.2
1.6
2.0
2.4
2.8
3.2
3.6
4.0
4.4
4.8
5.2
5.6
6.0
6.4
6.8
7.2
7.6
8.0
8.4
0.8
1.2
1.6
2.0
2.4
2.8
3.2
3.6
4.0
4.4
4.8
5.2
5.6
6.0
6.4
6.8
7.2
7.6
8.0
8.4
0.8
1.2
1.6
2.0
2.4
2.8
3.2
3.6
4.0
4.4
4.8
5.2
5.6
6.0
6.4
6.8
7.2
7.6
8.0
8.4
0.8
1.2
1.6
2.0
2.4
2.8
3.2
3.6
4.0
4.4
4.8
5.2
5.6
6.0
6.4
6.8
7.2
7.6
8.0
8.4
0.8
1.2
1.6
2.0
2.4
2.8
3.2
3.6
4.0
4.4
4.8
5.2
5.6
6.0
6.4
6.8
7.2
7.6
8.0
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\Users\Thomas\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.1.0.1371.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    174             else:
    175                 filename = fname
--> 176             exec compile(scripttext, filename, 'exec') in glob, loc
    177     else:
    178         def execfile(fname, *where):

C:\Users\Thomas\Dropbox\Thesis\Canopy files\Main.py in <module>()
    17     return Basismodel.calcAveragePi()
    18 
---> 19 doAll(100)
    20 
    21 #while(condition==0):

C:\Users\Thomas\Dropbox\Thesis\Canopy files\Main.py in doAll(c)
    12     Basismodel.setMu()
    13     Basismodel.setLambda()
---> 14     Basismodel.calcKappa()
    15     Basismodel.calcSumofprob()
    16     Basismodel.calcPi()

AttributeError: 'module' object has no attribute 'calcKappa'

Can anybody help?

4

1 回答 1

0
global c
global lamb
global pi
global kappa
global mu
global sumofkappa
global f

这些台词不会像你认为的那样做。global是放置在函数中以引用变量的全局声明的关键字。

例如

x = 42
def without_global():
    x = 9
    print(x)

def with_global():
    global x
    x = 13
    print(x)

print(x)
without_global()
print(x)
with_global()
print(x)

不过,我在这里遇到了完全不同的问题。

于 2013-10-15T19:48:07.660 回答