0
我不斷收到這個錯誤在我的井字棋遊戲類型錯誤:TypeError: unsupported operand type(s)
爲%
:「入門基於我是否使用函數或不
我有一個‘
「list'
和'int'
圈’變量來計算已經通過的圈數,並有充當董事會字符串列表
鑑於此代碼:
def x_or_o(t):
"""Check the number of turns for an even number and return a string."""
if t % 2 == 0:
return 'O'
else:
return 'X'
def place_char(i, t, brd):
"""Change the value of an item in the board list from an 'e' to an 'X' or
'O'."""
brd[int(i) - 1] = '{}'.format(x_or_o(t))
def turn(t, brd):
"""Read player input and print symbol onto the board."""
print("Player {}'s turn.".format(x_or_o(t)))
p_input = input('1-9: ')
check_quit(p_input)
if brd_empty(p_input, brd):
place_char(p_input, brd, x_or_o(t))
# brd[int(p_input) - 1] = '{}'.format(x_or_o(t))
...
當我在turn()
的if
聲明中使用place_char()
,我得到TypeError
。但是,當我從place_char()
直接複製並粘貼到turn()
時,我的代碼運行良好。
爲什麼我在使用該函數時遇到錯誤,但在使用函數中的代碼時卻沒有?
當't'是列表時出現錯誤。您的代碼不顯示它的值的來源。 –
當你用'place_char(p_input,brd,x_or_o(t))'調用函數時,參數的順序是錯誤的。 – martineau