0

だから私は自分が作っている三目並べゲームで複数の問題を抱えていましたが、ほとんどの問題の解決策を見つけました。しかし、今の問題は、読み取りエラーが発生することです

Traceback (most recent call last):
  File "/Users/user/Desktop/tic tac toe game.py", line 43, in <module>
    if board[input] != 'x' and board[input] !='o':
TypeError: list indices must be integers, not builtin_function_or_method" 

私がそれを実行しようとすると。問題が発生しているコードは次のとおりです。

if board[input] != 'x' and board[input] !='o':
        board[input] = 'x'

私は何をすべきかわからないので、木曜日にpythonを始めたばかりで、あまり賢くありません。しかし、すべての助けに感謝します(:ああ、私はpython3.2を持っています。

4

1 に答える 1

2

inputここでは整数ではないように見えますが、input()ここで使用した組み込み関数です。input変数名としても使用しないでください。

同じエラーを参照してください:

>>> a=[1,2,3,4]
>>> a[input]

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    a[input]
TypeError: list indices must be integers, not builtin_function_or_method

これを回避するには、次を使用します。

>>> Board=['a','x','o','b','c','d']
>>> n=input()#input returns a string,not integer
2
>>> if Board[int(n)]!='x' and Board[int(n)]!='o': #list expect  only integer indexes,convert n to integer first using
    Board[int(n)]='x'


>>> Board
['a', 'x', 'o', 'b', 'c', 'd']
于 2012-06-25T19:05:36.710 に答える