我想寫一個函數來確定一個字母是否出現在列表的某一行和列上。在列表中的這個位置上是否有一封信?
#Input:
lst2 = [['.', 'W', 'W', 'E', 'E'],
['.', '.', '.', '.', 'f'],
['A', 'A', 'A', '.', 'f']]
#Output: is_space_occupied(0, 1, lst2) should return True because 'W' is present on that spot
while is_space_occupied(1, 1, lst2) should return False because '.' is present on
that spot and not a letter.
這是我的代碼至今:
def letter_on_spot(row,col,lst):
A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
for row in lst:
for col in lst:
if col == A:
return True
else:
return False
編輯:我得到外面函數返回爲錯誤的返回true,我不知道如果我的功能才能正常工作
那麼,有什麼問題呢? – MooingRawr
我的代碼無法正常工作,並且出現錯誤我希望有人能夠看到可能出現的問題。我被告知這可以用2 for循環和if-else語句完成,但不能找出它 – warrior4223
在你的問題中發佈你的錯誤。 – MooingRawr