-2
我发现了密切相关的主题,但它没有回答我的问题。我的single_insert_or_delete
函数返回0
,1
或2
并基于该值我想在我的spelling_corrector
函数中使用它,但到目前为止我遇到了麻烦,当我使用第一个函数返回的if value:
作为我的主python函数的参数它不会接受这些值并且python返回None。我在这里错过什么?如何在新函数中使用布尔值函数返回值python
def spelling_corrector(s1,s2):
out_list=[]
def single_insert_or_delete(s1,s2):
a=s1.lower()
b=s2.lower()
if (len(a)==len(b))and (a==b):
return 0
elif len(a)>len(b):
if b in a:
if len(a)-len(b)==1:
return 1
else:
return 2
elif len(a)< len(b):
if a in b:
if len(b)-len(a)==1:
return 1
else:
count =0
for x in a:
if x in b:
count+=1
if len(b)-count==1:
return 1
else:
return 2
else:
return 2
这是主程序
s1= s1.split() #This will return list
for x in s1:
single_insert_or_delete(x,s2)
if 0:
out_list.append(x)
elif 1:
out_list.append(x)
elif 2:
out_list.append(x)
else:
out_list.append(x)
out_str=" ".join(out_list)
return out_str
测试输出
a="That is the Firs cas"
b=['that','first','case','car']
x=spelling_corrector(a,b)
print(x)
你的代码格式出错了。请参阅[Markdown帮助 - 代码和预格式化文本](http://stackoverflow.com/editing-help#code)并重试。 – Kevin
在'single_insert_or_delete'中检查你的逻辑,有许多分支不返回一个值(并因此落在最后,返回None),例如, 'single_insert_or_delete('abba','bb')' – thebjorn