Python 我的代码中没有返回

Python 我的代码中没有返回,python,python-2.7,Python,Python 2.7,如果我执行这段代码,它会部分工作。我尝试使用一个空字符串,代码运行正常。但有时当字符在字符串中时,它会告诉我False def isIn(char, aStr): """char is a single character and aStr is an alphabetized string. Returns: true if char is in aStr; false otherwise""" # base case: if aStr is an empty string if a

如果我执行这段代码,它会部分工作。我尝试使用一个空字符串,代码运行正常。但有时当字符在字符串中时,它会告诉我False

def isIn(char, aStr):
"""char is a single character and aStr is
an alphabetized string.
Returns: true if char is in aStr; false otherwise"""

# base case: if aStr is an empty string
    if aStr == '':
        return('The string is empty!')
        #return False
# base case: if aStr is a string of length 1
    if len(aStr) == 1:
        return aStr == char
# base case: see if the character in the middle of aStr is equal to the test char
    midIndex = len(aStr)/2
    midChar = aStr[midIndex]
    if char == midChar:
        return True
# Recursive case: if the test character is smaller than the middle character,recursively
# search on the first half of aStr
    elif char < midChar:
        return isIn(char, aStr[:midIndex])
# Otherwise the test character is larger than the middle character, so recursively
# search on the last half of aStr
    else:
        return isIn(char, aStr[midIndex:]) 

aStr = str(raw_input('Enter a word: '))
char = str(raw_input('Enter a character: '))
print(isIn(char,aStr))
def isIn(字符、应用程序代码):
“”“char是单个字符,而aStr是
按字母顺序排列的字符串。
返回:如果字符在aStr中,则返回true;否则返回false“”
#基本情况:如果aStr是空字符串
如果aStr='':
return('字符串为空!')
#返回错误
#基本情况:如果aStr是长度为1的字符串
如果len(aStr)==1:
返回asr==char
β基情况:在ASTC的中间字符是否等于测试字符
midIndex=len(aStr)/2
midChar=aStr[midIndex]
如果char==midChar:
返回真值
#递归大小写:如果测试字符小于中间字符,则递归
#搜索应用科技研究院的上半年
elif char
您的代码是正确的,您应该缩进它:

def isIn(char, aStr):
    """char is a single character and aStr is
    an alphabetized string.
    Returns: true if char is in aStr; false otherwise"""

    # base case: if aStr is an empty string
    if aStr == '':
    ...
然后只需使用以下工具进行测试:

>>> isIn('a', 'afdsf')
True
>>> print isIn('a', 'dfg')
False
顺便说一句,这在两行中完成了相同的操作:

def isIn(char, aStr):
    return char in sStr

您的代码是正确的,您应该缩进它:

def isIn(char, aStr):
    """char is a single character and aStr is
    an alphabetized string.
    Returns: true if char is in aStr; false otherwise"""

    # base case: if aStr is an empty string
    if aStr == '':
    ...
然后只需使用以下工具进行测试:

>>> isIn('a', 'afdsf')
True
>>> print isIn('a', 'dfg')
False
顺便说一句,这在两行中完成了相同的操作:

def isIn(char, aStr):
    return char in sStr

看起来您从未调用过定义的函数:

aStr = raw_input('Enter a word: ')  #raw_input already returns a string ,no need of str  
char = raw_input('Enter a character: ')
print isIn(char, aStr)                  #call the function to run it
演示:

函数定义不执行函数体;这仅在调用函数时执行

例如:

def func():   #function definition, when this is parsed it creates a function object
    return "you just executed func"

print func()    #execute or run the function
you just executed func           #output

看起来您从未调用过定义的函数:

aStr = raw_input('Enter a word: ')  #raw_input already returns a string ,no need of str  
char = raw_input('Enter a character: ')
print isIn(char, aStr)                  #call the function to run it
演示:

函数定义不执行函数体;这仅在调用函数时执行

例如:

def func():   #function definition, when this is parsed it creates a function object
    return "you just executed func"

print func()    #execute or run the function
you just executed func           #output


请修复缩进。为什么不在aStr中使用
char
?代码中缩进的ID?如果是这样,请参见(python教程)()如果没有缩进的docstring,该代码甚至无法编译,因此我假设这只是一个格式错误,但是如果我复制并粘贴代码,并且只修复docstring缩进,那么就可以了。请修复您的缩进。为什么不在aStr中使用
char
?标识代码中的缩进?如果是这样,请参阅(python教程]()如果没有缩进的docstring,代码甚至无法编译,所以我假设这只是一个格式错误,但是如果我复制并粘贴代码,只修复docstring缩进,它就可以正常工作。这是为什么被否决?这是对OP问题的正确答案为什么被否决?这是正确的!尽管由于缺少inden还不清楚示例的最后两行显然不是isIn函数和对isIn的调用(以及打印其结果)的一部分是使原始模块完整所需的。是的,我的答案也被错误地否决了。也许OP期待着其他东西。@david_doji那么是什么错误?@Ashwini Chaudhary我没有调用该函数。现在它给了我一些错误(字符串中有字符,它告诉我False)为什么会被否决?这是OP问题的正确答案为什么会被否决?这是正确的!虽然从缩进的缺失来看不清楚,但示例的最后两行显然不是isIn函数的一部分,也不是对isIn的调用(以及打印其结果)是使原始模块完整所需的。是的,我的答案也被错误地否决了。也许OP期待着其他东西。@david_doji那么是什么错误?@Ashwini Chaudhary我没有调用该函数。现在它给了我一些错误(字符串中有字符,它告诉我False)