以下是我的代码:
def find_first_occurance(s, c, start=0, end=len(s) ):
while start<end:
if s[start] ==c: # whether they are the same or not
return start
else:
start+=1
return -1
print(find_first_occurance("the days make us happy make us wise","s"))
我收到了错误name "s" is not defined
我有点理解发生了什么。另一方面,如果在Python中允许这个功能会很好,对吧?
答案 0 :(得分:0)
s
,因此错误。
您可以尝试在函数
中初始化def find_first_occurance(s, c, start=0, end=None ):
if end is None:
end = len(s)