我工作的程序和問題問:錯誤在我的代碼:應該返回一個布爾值,但返回的<class「NoneType」>
The first two ints are a row and column, and the third and fourth ints are another row and column. The last parameter is a symbol board. Return True if the path from the first row and column cell to the second row and column cell, including those two cells, is not completely empty, and return False otherwise. You may assume that the rows and columns given will form a horizontal or vertical path, not a diagonal path.
我的代碼看起來像這樣
MIN_SHIP_SIZE = 1
MAX_SHIP_SIZE = 10
MAX_BOARD_SIZE = 10
UNKNOWN = '-'
EMPTY = '.'
HIT = 'X'
MISS = 'M'
def is_occupied(row1, col1, row2, col2, symbol_board):
if row1 == row2: # Checks the condition if true
if col1 > col2: # Checks to see if column1 is greater than column 2
return coordinates_notempty(row1, col2, col1, symbol_board)
else:
return coordinates_notempty(row1, col1, col2, symbol_board)
elif col1 == col2:
if row1 > row2:
return coordinates_notempty(col1, row2, row1, symbol_board)
else:
return coordinates_notempty(col1, row1, row2, symbol_board)
coordinates_notempty
是一個輔助功能,看起來像這樣:
def coordinates_notempty(c, c2, c3, symbol_board):
for i in range(c2, c3):
if symbol_board[c][i] == EMPTY:
return False
return True
當我在空閒運行代碼我得到的是S上的錯誤ays應該返回一個bool,但返回<class 'NoneType'>
,但我不明白爲什麼。
當我修復語法錯誤後,仍然收到應該返回布爾值,但返回錯誤。有人建議增加一個else語句,當我做了
def is_occupied(row1, col1, row2, col2, symbol_board):
if row1 == row2:
if col1 > col2:
return coordinates_notempty(row1, col2, col1, symbol_board)
else:
return coordinates_notempty(row1, col1, col2, symbol_board)
elif col1 == col2:
if row1 > row2:
return coordinates_notempty(col1, row2, row1, symbol_board)
else:
return coordinates_notempty(col1, row1, row2, symbol_board)
else:
return True
我收到一個錯誤,類型錯誤:列表索引必須是整數,而不是元組
在Python中,true和false用「True」和「False」表示。他們必須大寫。 – Shashank 2015-03-24 21:49:43
^我希望在此基礎上有一個NameError。請提供[MCVE](http://stackoverflow.com/help/mcve)。 – jonrsharpe 2015-03-24 21:50:34
我看不出我怎麼可能更加和平 – JerryMichaels 2015-03-24 22:54:50