Python输入TypeError:“ _ io.TextIOWrapper”对象不可调用

时间:2018-09-21 11:49:16

标签: python input typeerror

有人可以解释一下为什么在下面的代码的TypeError: '_io.TextIOWrapper' object is not callable函数中使用输入语句得到Maze吗?

我在功能之外测试了它,并且工作正常。 SO中没有任何关于此错误的答案似乎可以解决我的问题。

# This program traverses a maze using a stack.

from sq import Stack              # import a Stack type from sq.py

MAZE_SIZE = 10                    # define the size of the maze

def PrintMaze(maze):              # an auxilliary function that prints a maze
    for row in range(MAZE_SIZE):
        print(maze[row], end='')
    print()

def InBounds(xxx_todo_changeme):              # an auxillary function that determines if (x,y) is on the maze
    (x,y) = xxx_todo_changeme
    return (0 <= x < MAZE_SIZE) and (0 <= y < MAZE_SIZE)

def Maze(maze, start):          # traverse 'maze' from starting coordinates 'start'
    s = Stack()                  # create a new Stack named s
    s.push(start);               # push the start coordinates onto s
    while not s.isEmpty():       # loop while s is not empty
        (x, y) = s.pop()          # pop a coordinate off s into the tuple (x,y)
        input("press Enter to continue ")
        if InBounds((x,y)):         # if (x,y) is on the maze then
            if maze[x][y] == 'G':    # if (x,y) is the goal then
                s.empty()             # empty the stack because we're done
            elif maze[x][y] == ' ':  # else if (x,y) is an undiscovered coordinate
                maze[x] = maze[x][:y] + 'D' + maze[x][y+1:]  # mark (x,y) discovered with 'D'
                PrintMaze(maze);      # print the maze to show progress so far
                s.push((x+1, y))      # push right neighbor onto stack s
                s.push((x-1, y))      # push left neighbor onto stack s
                s.push((x, y-1))      # push upper neighbor onto stack s
                s.push((x, y+1))      # push lower neighbor onto stack s

# The following can be used to create a maze and traverse it:

import sys
input("press Enter to continue ")
input = open('maze.dat', 'r')     # open the file 'maze.dat' for reading
maze = input.readlines();         # read the file into maze
Maze(maze, (0,0))                 # traverse the maze starting at (0,0)

1 个答案:

答案 0 :(得分:1)

因为您在代码中重新定义了input

import sys
input("press Enter to continue ")

# HERE !
input = open('maze.dat', 'r')     # open the file 'maze.dat' for reading
maze = input.readlines();         # read the file into maze
Maze(maze, (0,0)) 

将变量input的名称更改为其他名称。