模块在Python中没有属性

3lxsmp7m  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(164)

我正在从一个模块导入变量,但它指出没有已导入变量的示例。有两个文件:eqparse和main。我将包含所有杂项代码(可能不相关)以防万一。有注解可以帮助导航代码。
主要代码:

import graphics
import eqparse
# NOTE: For equation mapping, it will input a string and convert the data to a table. 
# After, you take in the table coords and map each point 

# dump vars here #
xypm = 0

counter = eqparse.counter

patternFind = eqparse.patternFind

indexlist = eqparse.indexlist

# profile initialization #
def init(name, nxsize, nysize, noffset, nzoom):
    global zoom
    zoom = nzoom
    global xsize
    xsize = nxsize
    global eqtype
    eqtype = bool(1)
    global ysize
    ysize = nysize
    global win
    win = GraphWin(name, nxsize, nysize)
    global offset
    offset = noffset
    global xymark
    xymark = (nzoom * -1)/2 
    global txsize
    txsize = nxsize/2
    global tysize
    tysize = nysize/2
    global txymark
    txymark = xymark * -1 + 1
    global xp1
    xp1 = Point(xypm,(txsize) + offset)
    global yp1
    yp1 = Point((tysize) + offset, xypm)
    global xp2
    xp2 = Point(xypm,(txsize) - offset)
    global yp2
    yp2 = Point((tysize) - offset, xypm)

# starting procedure #
def startUp():
        # xy lines #
    global xymark
    global xp1
    global xp2
    global yp1
    global yp2
    global xypm
    xtrace = Line(Point(0,tysize), Point(xsize,tysize))
    ytrace = Line(Point(txsize,0), Point(txsize,ysize))
    xtrace.draw(win)
    ytrace.draw(win)

        # grid drawer #
    while xymark < txymark:
        
        txline = Line(xp1, xp2)
        tyline = Line(yp1, yp2)
        txline.draw(win)
        tyline.draw(win)
        xypm = xypm + xsize/zoom
        xp1 = Point(xypm,(tysize) + offset)
        yp1 = Point((txsize) + offset, xypm)
        xp2 = Point(xypm,(tysize) - offset)
        yp2 = Point((txsize) - offset, xypm)
        xymark = xymark + 1

        # code for ending mark #
    Line(Point(txsize - offset, ysize - 1), Point(txsize + offset, ysize - 1)).draw(win)
    Line(Point(xsize - 1,tysize - offset), Point(xsize - 1, tysize + offset)).draw(win)

# point drawing #
def drawCo(nx, ny):
    pCircle = Circle(Point(xsize/zoom * (nx) + xsize/2, ysize - (ysize/zoom * (ny) + ysize/2)), 3)
    pCircle.setFill("black")
    pCircle.draw(win)

# main #
print("Checkpoint 1")
init("test",500, 500, 10, 20)
print("Checkpoint 2")
startUp()
print("Checkpoint 3")
drawCo(3,5)
print("Checkpoint 4")
patternFind("Dung","DungBeatlesbeingadungbeetle")
print("Checkpoint 5")
print(counter)
print("Checkpoint 6")
for x in range(len(indexlist)):
    print(indexlist[x])
print("Checkpoint 7")

# exit function #
win.getMouse()
win.close()

eqparse代码:

#   eqparse will look for certain patterns in strings, generate an equation,
#   and create a table that a drawing function can read

global indexlist

global counter

# convert string to valid equation data

def convStr(input):
    if input[0] == 'y':
        print("Linear")
        
    elif input[0] == 'r':
        print("Polar")
    else:
        print("Use Equation type on left side")

# subroutine that will be used to find patterns in a sequence #

def patternFind(pattern, input):

    indexlist = []
    
    counter = 0

    l = len(pattern)

    tstr =""

    if l > len(input):
        pass
    else:
        i = 0
        j = len(input) - len(pattern) + 1
        k=0
        while j > 0:
            tstr = ""
            i=0
            while len(tstr) < l:
                tstr = tstr + input[i + k]
                i = i + 1
            if tstr == pattern:
                counter = counter + 1
                indexlist.append(i+k)
            else: 
                pass
            j = j - 1
            k=k+1

输出/错误代码:

module 'eqparse' has no variable 'counter'

任何形式的帮助都很感激。
我尝试更改变量名,以防它已经在Python的标准库或图形库中使用,但没有任何变化。

vc6uscn9

vc6uscn91#

在模块级别定义变量时,
你在定义一个全局变量。
在函数内部定义变量时,
你在定义一个局部变量。
如果在模块级别上使用foo = 7
然后在函数中使用foo = 17
您正在更改 * 本地 * 变量foo
其与 * 全局 * 变量foo * 无关 *,
但是如果在函数中输入foo = 17之前
你在函数中输入global foo
那么你就说“当我定义foo的时候,我是在重新定义全局变量foo”。

#   eqparse will look for certain patterns in strings, generate an equation,
#   and create a table that a drawing function can read

indexlist = []
counter = 0

# convert string to valid equation data

def convStr(input):
    if input[0] == 'y':
        print("Linear")
        
    elif input[0] == 'r':
        print("Polar")
    else:
        print("Use Equation type on left side")

# subroutine that will be used to find patterns in a sequence #

def patternFind(pattern, input):
    global indexlist
    global counter

    l = len(pattern)

    tstr =""

    if l > len(input):
        pass
    else:
        i = 0
        j = len(input) - len(pattern) + 1
        k=0
        while j > 0:
            tstr = ""
            i=0
            while len(tstr) < l:
                tstr = tstr + input[i + k]
                i = i + 1
            if tstr == pattern:
                counter = counter + 1
                indexlist.append(i+k)
            else: 
                pass
            j = j - 1
            k=k+1

相关问题