Reputation: 13
This is the problem I'm running into, instead of returning new_word and printing it out it just prints 'None'
text = "hey hey hey,hey"
word = 'hey'
def censor(text,word):
new_word = ""
count = 0
if word in text:
latter_count = ((text.index(word))+len(word))
while count < text.index(word):
new_word+= text[count]
count += 1
for i in range(len(word)):
new_word += '*'
while latter_count < len(text) :
new_word += text[latter_count]
latter_count += 1
if word in new_word :
censor(new_word,word)
else :
return new_word
print censor(text,word)
Upvotes: 1
Views: 1206
Reputation: 1142
If the function hits the end without hitting a "return" statement, it's the same as "return None":
def censor(text,word):
new_word = ""
count = 0
if word in text:
latter_count = ((text.index(word))+len(word))
while count < text.index(word):
new_word+= text[count]
count += 1
for i in range(len(word)):
new_word += '*'
while latter_count < len(text) :
new_word += text[latter_count]
latter_count += 1
if word in new_word :
censor(new_word,word) # probably want to return here
else : # don't need this else if you return in the if branch
return new_word
# what do you want to return in this case?
return None
Upvotes: 0
Reputation: 59984
A function returns None
if there is no return statement.
Probably while doing recursion, if word in text:
is False, and so there is nothing to return. You also did not return the recursive step. You must return censor(new_word,word)
Upvotes: 4
Reputation: 251408
You're not returning in the first branch of the if
toward the end. Change that to
if word in new_word:
return censor(new_word,word)
Your function will also return None if word in text
is false, so you might want to add an else
at the end to return an empty string or some other default value in that case.
Upvotes: 2