|
|
|
|
 求助作业 - kickky [ 2006-07-27 23:17 | 924 byte(s)]
 Re: 求助作业 - limodou [ 2006-07-31 09:22 | 30 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
用递归写一个function可以找到相应的index
如果找不到,需要return False
可是phthon 把false当成0处理了
应该怎么办呢?
谢谢啦
#- recursive function search [5 marks] -#
def search(l,key):
"""
locates key in list l. if present, returns location as an index;
else returns False.
PRE: l is a list.
POST: l is unchanged; returns i such that l[i] == key; False otherwise.
"""
if l[0] == key:
return 0
elif len(l) != 1:
del l[0]
return search(l,key) + 1
else:
return False
#- sample main -#
l1 = [1, '2', 'x', 5, -2]
#print search(l1, 'x') # should output: "2"
print search(l1, 2) # should output: "False"
|
|
|
[Original]
[Print]
[Top]
|
|
|