开发者

printSoln module problem

Hi I found in book:Numerical Methods in engineering with Python the module run_kut5, but for that module I need module printSoln, all provided in the book. Now I cp the code, made necessary line adjustments and so. The code looks like:

  # -*- coding: cp1250 -*-
## module printSoln
''' printSoln(X,Y,freq).
    Prints X and Y returned from the differential
    equation solvers using printput frequency ’freq’.
        freq = n prints every nth step.
        freq = 0 prints initial and final values only.
'''

def printSoln(X,Y,freq):

    def printHead(n):
        print "\n  x ",
        for i in range (n):
            print " y[",i,"] ",
        print

    def printLine(x,y,n):
        print "%13.4e"% x,f
        for i in range (n):
            print "%13.4e"% y[i],
        print

    m = len(Y)
    try: n = len(Y[0])
    except TypeError: n = 1
    if freq == 0: freq = m
    printHead(n)
    for i in range(0,m,freq):
        printLine(X[i],Y[i],n)
    if i != m - 1: printLine(X[m - 1],Y[m - 1],n)

Now, when I run the program it says:

line 24, in <module>
    m = len(Y)
NameError: name 'Y' is not define开发者_Go百科d

But I cp'd from the book :\ So now when I call the run_kut module I get the same error, no Y defined in printSoln...

I'm trying to figure this out but I suck :(

Help, please...


I would guess it's a tabs/spaces problem - check that you don't have mixed tabs and spaces in the indentation.

EDIT: If it's not indentation, and since your error message contains "<module>" and not a filename, I'm guessing you're pasting this into an interactive interpreter.

Instead, you should paste the code into a file called printsoln.py, and then run this in the interactive interpreter:

from printsoln import printSoln

If you still want to paste it all in the interpreter then you'll probably need to remove the blank lines - those after def printSoln and after each internal function. The interactive interpreter uses blank lines to figure out when you're done with a multi-line definition, and tries to evaluate m = len(Y) outside the context of function printSoln. In this context, variable Y doesn't exist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜