2016-05-13 54 views
1

所以我嘗試使用我的代碼時出現此錯誤。我不明白爲什麼我會得到這個:TypeError:'builtin_function_or_method'對象不是可訂閱的

File "/Users/max/Desktop/Code/Python/game.py", line 30, in <module> 
     lineone.remove[0]  #or whatever number I use 
    TypeError: 'builtin_function_or_method' object is not subscriptable 

我的代碼是

lineone = ['0', '0', '0', '0', '0', '0', '0'] 
linetwo = ['0', '0', '0', '0', '0'] 
linethree = ['0', '0', '0'] 
lineoneX = ['X', 'X', 'X', 'X', 'X', 'X', 'X'] 
linetwoX = ['X', 'X', 'X', 'X', 'X'] 
linethreeX = ['X', 'X', 'X'] 
notfirst = 0 
player1 = input('''Enter player 1's name ''') 
player2 = input('''Enter player 2's name ''') 
print('The person who takes the last stone wins!') 
print(lineone[0], lineone[1], lineone[2], lineone[3], lineone[4], lineone[5], lineone[6]) 
print(linetwo[0], linetwo[1], linetwo[2], linetwo[3], linetwo[4]) 
print(linethree[0], linethree[1], linethree[2]) 

while True: 
    #WTD means -What to Delete 
    WTD = input('Type the row number, then the amount of stones you want to take, in the format 1, 1. ') 
      # Line One 
    if WTD == '1, 1': 
      lineone.remove[0] 
    if WTD == '1, 2': 
      lineone.remove[1] 
    if WTD == '1, 3': 
      lineone.remove[2] 
    if WTD == '1, 4': 
      lineone.remove[3] 
    if WTD == '1, 5': 
      lineone.remove[4] 
    if WTD == '1, 6': 
      lineone.remove[5] 
    if WTD == '1, 7': 
      lineone.remove[6] 
      # Line Two 
    if WTD == '2, 1': 
      linetwo.remove[0] 
    if WTD == '2, 2': 
      linetwo.remove[1] 
    if WTD == '2, 3': 
      linetwo.remove[2] 
    if WTD == '2, 4': 
      linetwo.remove[3] 
    if WTD == '2, 5': 
      linetwo.remove[4] 
      # Line Three 
    if WTD == '3, 1': 
      linetwo.remove[0] 
    if WTD == '3, 2': 
      linetwo.remove[1] 
    if WTD == '2, 3': 
      linetwo.remove[2] 
    print(lineone) 
    print(linetwo) 
    print(linethree) 

我已經看着很多其他地方,但我不明白,爲什麼這是行不通的。我正在使用方括號,我爲第一項使用0而不是1。 所以請幫助, 在此先感謝!

+0

'remove'是一種方法;你用括號括起來而不是括號。它也沒有做你想做的事情,所以你在改變方括號後仍然會遇到問題。 – user2357112

+0

你是什麼意思:它也沒有做你想做的事 – Maximus

+0

你試圖從列表中刪除指定數量的項目。 '刪除'不這樣做。 – user2357112

回答

2

問題是,你正在嘗試使用刪除像一個列表或字典,而它是一個函數,這就是爲什麼你得到TypeError: 'builtin_function_or_method' object is not subscriptable。如果你想刪除第一個'0'元素,或者例如,你應該嘗試lineone.remove('0')lineone.pop(0)如果你想刪除第一個元素。詳情請查閱docs

相關問題