2016-11-17 85 views
0

在我繼續完成我的tic tac toe遊戲的過程中,我試着寫了一個函數來檢查獲勝者。遊戲看起來是這樣的: 規劃委員會提出的名單列表:姓名未定義錯誤

lst = ['1','2','3','4','5','6','7','8','9'] 

def board(): 
print (lst[0:3]) 
print (lst[3:6]) 
print (lst[6:]) 

['1', '2', '3'] 
['4', '5', '6'] 
['7', '8', '9'] 

then this not so elegant function to check for a winner: 

def conclusion(): 
if all('o' == item for item in (lst[0],lst[1],lst[2])): 
    print('Player 2 wins!') 
elif all('o' == item for item in (lst[3],lst[4],lst[5])): 
    print('Player 2 wins!') 
elif all('o' == item for item in (lst[6],lst[7],lst[8])): 
    print('Player 2 wins') 
elif all('o' == item for item in (lst[0],lst[3],lst[6])): 
    print('Player 2 wins') 
elif all('o' == item for item in (lst[1],lst[4],lst[7])): 
    print('Player 2 wins') 
elif all('o' == item for item in (lst[3],lst[6],lst[9])): 
    print('Player 2 wins') 
elif all('o' == item for item in (lst[0],lst[4],lst[8])): 
    print('Player 2 wins') 
elif all('o' == item for item in (lst[2],lst[4],lst[6])): 
    print('Player 2 wins') 
elif all('x' == item for item in (lst[0],lst[1],lst[2])): 
    print('Player 1 wins!') 
elif all('x' == item for item in (lst[3],lst[4],lst[5])): 
    print('Player 1 wins!') 
elif all('x' == item for item in (lst[6],lst[7],lst[8])): 
    print('Player 1 wins') 
elif all('x' == item for item in (lst[0],lst[3],lst[6])): 
    print('Player 1 wins') 
elif all('x' == item for item in (lst[1],lst[4],lst[7])): 
    print('Player 1 wins') 
elif all('x' == item for item in (lst[3],lst[6],lst[9])): 
    print('Player 1 wins') 
elif all('x' == item for item in (lst[0],lst[4],lst[8])): 
    print('Player 1 wins') 
elif all('x' == item for item in (lst[2],lst[4],lst[6])): 
    print('Player 1 wins') 
else: 
    pass 

那麼這些功能的遊戲:

def move2(): 
conclusion() 
move2=(input('Player 2: Type a number!')) 
for x in lst: 
    if move2 == x: 
     lst[int(move2)-1] = 'o' 
     board() 
     move() 
    elif move2.isdigit() and move2 not in lst: 
     print('Not that number dipshit!') 
     break 
     board() 
     move2() 
    elif not move2.isdigit(): 
     print('Not that number dipshit!') 
     break 
     board() 
     move2() 


def move(): 
conclucion() 
move1=(input('Player 1: Type a number!')) 
for x in lst: 
    if move1 == x: 
     lst[int(move1)-1] = 'x' 
     board() 
     move2() 
    elif move1.isdigit() and move1 not in lst: 
     print('Not that number dipshit!') 
     board() 
     move() 
     break 
    elif not move1.isdigit(): 
     print('Not that number dipshit!') 
     board() 
     move() 
     break 

問題是,當我嘗試運行它,我不斷收到此錯誤:

<ipython-input-27-8688e8182de8> in move() 
     1 def move(): 
----> 2  conclucion() 
     3  move1=(input('Player 1: Type a number!')) 
     4  for x in lst: 
     5   if move1 == x: 

NameError: name 'conclucion' is not defined 

任何想法?精簡這一建議也是非常受歡迎的。

+1

'def move(): conclucion()'是否因爲存在拼寫錯誤?將最後一個'c'改爲's',它應該起作用。 –

+1

你的功能被稱爲'結論',而不是'結束' – schwobaseggl

+0

謝謝,這是令人尷尬的。但現在它說'列表索引超出範圍'@Noah Christopher – Boris

回答

3

你把「結論」拼寫錯了,作爲「結論」。