jeudi 21 octobre 2021

nested if python executed in one case but not in another similar condition

Hi please find my code below:

def isIn(char, aStr):
    #char: a single character
    #aStr: an alphabetized string
    #returns: True if char is in aStr; False otherwise
    length=len(aStr)
    print(length, aStr)     #check
    if length == 0:
        return False
    elif length == 1:
        if char == aStr:
            return True
        else:
            return False
    elif char==aStr[int(length/2)]:
        return True
    elif char<aStr[int(length/2)]:
        print("checked")
        aStr=aStr[0:int(length/2)]
        isIn(char,aStr)
    elif char>aStr[int(length/2)]:
        isIn(char,aStr[int(length/2):-1])

if i run with

a='b'
s='b'
v=isIn(a,s)
print(v)

prints out True at the last line

if i run with

a='b'
s='bcd'
v=isIn(a,s)
print(v)

prints out None when it should print True. Any ideas?

Aucun commentaire:

Enregistrer un commentaire