Home > Backend Development > Python Tutorial > Why Does My Recursive Function Return `None` Instead of `True` When It Finds the Character?

Why Does My Recursive Function Return `None` Instead of `True` When It Finds the Character?

Susan Sarandon
Release: 2024-10-26 04:04:27
Original
408 people have browsed it

Why Does My Recursive Function Return `None` Instead of `True` When It Finds the Character?

Why Does Recursive Code Return None?

In the provided code snippet, a recursive function, isIn, is defined to determine whether a given character exists within a string. However, it consistently returns None instead of the expected True value when the character is found within the string.

The code performs a binary search on the string, repeatedly dividing it in half. When the character is found at the midpoint, it prints a message indicating its location, but it fails to return True.

To rectify this issue, a return statement should be added to the last line of the function, as seen below:

<code class="python">return isIn(char, aStr)</code>
Copy after login

Without this return statement, the function simply returns None when it terminates without encountering a return statement. By adding this return, the function can properly return True when the character is located within the string.

The above is the detailed content of Why Does My Recursive Function Return `None` Instead of `True` When It Finds the Character?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template