【问题标题】:int object is unscriptable Pythonint 对象是无法编写脚本的 Python
【发布时间】:2012-08-02 16:25:53
【问题描述】:

我正在尝试制作 Battleship 用于练习,单人游戏很成功……当时只有一个玩家、一组船和一个棋盘:P

知道为什么这会给我一个“int object is unscriptable”错误吗???

这里是 Board 类。好吧,还是有一些:

class Board:
    'Game Board'
    topMarkers = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
    sideMarkers = list(range(0, 26))


    def __init__(self,h,w): #Makes the map
        self.height = h
        self.width = w

        self.xMarkers = []
        self.yMarkers = []
        self.hitList = []
        self.hitListCopy = []

        self.boardSpace = '         '

        wave = '~'
        self.row = []
        self.column = []
        #self.column = wave * self.width # If width is 4, column is now '~~~~'

        for c in range(self.width):
            self.column.append(wave)
            self.xMarkers.append(Board.topMarkers[c])

        for r in range(self.height): #
            self.row.append(self.column[:]) #Use the slice to make copies
            self.yMarkers.append(Board.sideMarkers[r])



    def showGrid(self):
        print self.boardSpace + '  ' +  ' '.join(self.xMarkers)
        for i in range(self.height):
            print self.boardSpace + str(self.yMarkers[i]) + ' ' + '-'.join(self.row[i])

这是实际运行它的代码,导致它的代码部分位于底部的 while 和 for 循环中,它在注释中说显示您的地图的网格。

p1 = [[Board(7,7), Board(7,7)], Move(), Connection(1, netEnable)]
p2 = [[Board(7,7), Board(7,7)], Move(), Connection(2, netEnable)]
#p3 = [[Board(7,7), Board(7,7)], Move(), Connection(3, netEnable)]

#Like this p1 = [[theirBoard, attackBoard], Moves, Connection]



#p = [p1,p2,p3]
p = [p1,p2]


ships = [Ship(0),Ship(0),Ship(0)]
ships2 = [Ship(0),Ship(0),Ship(0)]

numOfPlayers = len(p)


youPlayer = int(raw_input('Which player are you? 1,2,3:   '))

youPlayer -= 1



boardr = p[youPlayer][0][1].getRowData()
boardM = p[youPlayer][0][0].getMarkerData()
#raw_input(boardr)
raw_input(boardM)



#Set Ships Dialog.
for i in range(len(ships)):
    p[youPlayer][0][1].showGrid() 
    ships[i].setShip(boardr, boardM)


shipPosAll = [[],[],[]]    

for i in range(len(ships)):
    shipPosAll[youPlayer].append(ships[i].getShip()) #USE THIS INFO TO FEED TO THE BOARD TO TELL IT WHERE SHIPS ARE SO YOU KNOW WHERE HITS ARE.

print 'shipPos at line 382 : %s' % (shipPosAll[youPlayer])




#INIT, DO ONLY ONCE

for i in range(numOfPlayers): 
    print p[i][2].GetTime()

#MAIN LOOP
while gameNotOver:

    for i in range(numOfPlayers):
        playersCheck = [0,1]
        del playersCheck[youPlayer]

        print 'Player %s turn' % (i)
        print 'Here are your ships'
        #raw_input (p[i][0][1])
        p[i][0][1].showGrid() #Show the grid of the player (TheirShips) HERES WHERE THE PROBLEM IS!!!!

        print 'Where do you want to attack?'
        p[i][0][0].showGrid() #Show the grid of the player (AttackShips)

        hits = p[i][0][0].getHitList()
        alreadyMade = False
        while alreadyMade == False:
            coords = p[i][0][0].chooseMove()
            alreadyMade = p[i][1].addMove(coords) #Returns true if move accepted, otherwise false

        lastMove = p[i][1].getMove()
        print 'The move you made was %s' % (lastMove)
        p[i][2].Send(lastMove)
        changeCoords = p[i][2].Check()
        p[i][0][0].changeRow('B')

        for p in playersCheck: #Check every player who is not you. You were deleted at teh beginning.
            checkForSunk(shipPosAll[p], hits)
            print 'Current Attacked Players shipPos: %s' % (shipPosAll[p])
        #p1Site.Send(coords)
        #print 'Thing %s' % (shipPos[i])
            if isGameOver(shipPosAll[p]): #PROBLEM.
                gameNotOver = False
                break

    for i in range(numOfPlayers):
        print p[i][1].getAllMoves()


print 'All Ships Sunk. The End. Wat more you want from mme its 3:48am'

谢谢

这是我得到的确切信息。

Traceback(最近一次调用最后一次):文件“name/battleship_wClasses.py”, line 431, in p[i][0][1].showGrid() #显示网格 player (TheirShips) TypeError: 'int' object is unsubscriptable –

【问题讨论】:

  • 请发布确切的错误信息,包括行号。
  • 哇,这是很多代码,您能否提供完整的回溯以及在代码中标识问题行的注释。
  • Traceback (most recent call last): File "name/battleship_wClasses.py", line 431, in p[i][0][1].showGrid() #显示网格玩家(TheirShips)类型错误:'int'对象不可订阅
  • 而这甚至不是完整的代码...全部用于终端战舰游戏...我休息了一周,现在忘记了它是如何工作的:P....无论如何,代码中有注释

标签: python list error-handling


【解决方案1】:

p 有两个(重叠的)矛盾定义:

p = [p1,p2]

    playersCheck = [0,1]
    for p in playersCheck:

p[i][0][1].showGrid() 适用于第一个定义,但在为 p 分配第二个定义的整数值时失败。

【讨论】:

  • 哦,哇,这是第一次 for 循环实际上干扰了变量...我一直假设 for 循环迭代变量只存在于循环期间。谢谢
  • 是的,这是一个 Python 陷阱——for-loops 将它们的迭代变量“泄漏”到周围的作用域中。
猜你喜欢
  • 2010-12-14
  • 2021-08-18
  • 1970-01-01
  • 2012-11-15
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多