我在寫一個簡單的搜索算法。以下是我的代碼。替代Len函數
def search(list_data,target_char):
found = False
position = 0
while position < len(list_data) and not found:
if list_data[position] == target_char:
found = True
position += 1
return found
但是我不應該使用len()或任何其他內置函數。我怎麼能這樣做?
'而真實:... break'?你還需要像你一樣手動增加位置,並且需要一個'try-except'塊來捕捉'IndexError' –