2017-06-10 40 views
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()時,我的代碼運行良好。

爲什麼我在使用該函數時遇到錯誤,但在使用函數中的代碼時卻沒有?

+0

當't'是列表時出現錯誤。您的代碼不顯示它的值的來源。 –

+1

當你用'place_char(p_input,brd,x_or_o(t))'調用函數時,參數的順序是錯誤的。 – martineau

回答

0
def place_char(i, brd, xo): 
    """Change the value of an item in the board list from an 'e' to an 'X' or 
    'O'.""" 
    brd[int(i) - 1] = '{}'.format(xo) 

你可以嘗試用這個替換你的place_char方法。

錯誤在你的方法:

    在你的方法簽名 place_char(i, t, brd)其中的順序是不同的
  • 你與tplace_char再打電話給x_or_o但你的x_or_o(t)輸出已經傳遞到place_char方法
+0

如果這有幫助,請接受它並將其標記爲正確答案 – pramod

相關問題