2013-09-21 64 views
9

我在代碼中收到以下錯誤。我試圖做一個迷宮解算器和我得到一個錯誤,指出:TypeError:'模塊'對象不能爲python對象調用

Traceback (most recent call last): 
    File "./parseMaze.py", line 29, in <module> 
    m = maze() 
TypeError: 'module' object is not callable 

我試圖創建一個名爲m一個maze對象,但顯然我做錯了。

parseMaze.py

#!/user/bin/env python 

import sys 
import cell 
import maze 
import array 

# open file and parse characters 
with open(sys.argv[-1]) as f: 
# local variables 
    x = 0 # x length 
    y = 0 # y length 
    char = [] # array to hold the character through maze 
    iCell = []# not sure if I need 
# go through file 
    while True: 
    c = f.read(1) 
    if not c: 
     break 
    char.append(c) 
    if c == '\n': 
     y += 1 
    if c != '\n': 
     x += 1 
    print y 
    x = x/y 
    print x 

    m = maze() 
    m.setDim(x,y) 
    for i in range (len(char)): 
    if char(i) == ' ': 
     m.addCell(i, 0) 
    elif char(i) == '%': 
     m.addCell(i, 1) 
    elif char(i) == 'P': 
     m.addCell(i, 2) 
    elif char(i) == '.': 
     m.addCell(i, 3) 
    else: 
     print "do newline" 
    print str(m.cells) 

這裏寫下這行是我maze.py文件,其中包含了迷宮類:

#! /user/bin/env python 

class maze: 

    w = 0 
    h = 0 
    size = 0 
    cells =[] 

# width and height variables of the maze 
    def _init_(self): 
    w = 0 
    h = 0 
    size = 0 
    cells =[] 


# set dimensions of maze 
    def _init_(self, width, height): 
    self.w = width 
    self.w = height 
    self.size = width*height 

# find index based off row major order 
    def findRowMajor(self, x, y): 
    return (y*w)+x 

# add a cell to the maze 
    def addCell(self, index, state): 
    cells.append(cell(index, state)) 

它是什麼,我做錯了什麼?

回答

38

它應該是maze.maze()而不是maze()

或者您可以將您的import聲明更改爲from maze import maze

+0

謝謝!第一個迷宮引用文件,第二個引用該類? – user2604504

+1

@ user2604504是的。但從技術上講,它引用的是「模塊」(它是用文件內容構建的),而不是「文件」。 –

0

問題是導入聲明,你只能導入一個類而不是模塊。 '進口迷宮'是錯誤的,而是使用 '從迷宮進口迷宮'

相關問題