Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

python - 'int' object is not callable only appears randomly

Sometimes this code works just fine and runs through, but other times it throws the int object not callable error. I am not real sure as to why it is doing so.

for ship in ships:
    vert_or_horz = randint(0,100) % 2
    for size in range(ship.size):
        if size == 0:
            ship.location.append((random_row(board),random_col(board)))
        else:
            # This is the horizontal placing
            if vert_or_horz != 0 and ship.size > 1:
                ship.location.append((ship.location[0][0], 
                                      ship.location[0][1] + size))
                while(ship.location[size][1] > len(board[0])) or 
                     (ship.location[size][1] < 0):
                    if ship.location[size][1] > len(board[0]):

                        ship.location[size][1]((ship.location[0][0], 
                                              ship.location[0][1] - size))
                    if ship.location[size][1] < 0:
                        ship.location[size][1]((ship.location[0][0], 
                                            ship.location[0][1] + size))

            # This is the vertical placing
            if vert_or_horz == 0 and ship.size > 1:
                ship.location.append((ship.location[0][0] + size, 
                                      ship.location[0][1]))
                while(ship.location[size][1] > len(board[0])) or 
                     (ship.location[size][1] < 0):
                    if ship.location[size][1] > len(board[0]):
                        ship.location[size][1] 
                            ((ship.location[0][0] - size, 
                              ship.location[0][1]))
                    if ship.location[size][1] < 0:
                        ship.location[size][1] 
                            ((ship.location[0][0] + size, 
                              ship.location[0][1]))

Here is the traceback:

Traceback (most recent call last):
  File "python", line 217, in <module>
  File "python", line 124, in create_med_game
    ship.location[size][1]((ship.location[0][0], 
TypeError: 'int' object is not callable
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are trying to call (use the ..(...) syntax) on an integer here:

ship.location[size][1]((ship.location[0][0], 
                        ship.location[0][1] - size))

ship.location[size][1] is an integer, and you are trying to call it by passing in the tuple (ship.location[0][0], ship.location[0][1] - size) as the arguments.

Your code makes this mistake in more than one place; other examples include

ship.location[size][1]((ship.location[0][0], 
                    ship.location[0][1] + size))

and

ship.location[size][1] 
    ((ship.location[0][0] - size, 
      ship.location[0][1]))

and

ship.location[size][1] 
    ((ship.location[0][0] + size, 
      ship.location[0][1]))

The backslash there still joins the physical line into a logical line, so the (...) calls still apply to the integers.

It is not clear to me what you wanted to do with that expression, however, so I can't offer any remedy here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...