python - No return in my code -


if execute piece of code, works partially. tried empty string , code works. tells me false when character in string!

def isin(char, astr): """char single character , astr alphabetized string. returns: true if char in astr; false otherwise"""  # base case: if astr empty string     if astr == '':         return('the string empty!')         #return false # base case: if astr string of length 1     if len(astr) == 1:         return astr == char # base case: see if character in middle of astr equal test char     midindex = len(astr)/2     midchar = astr[midindex]     if char == midchar:         return true # recursive case: if test character smaller middle character,recursively # search on first half of astr     elif char < midchar:         return isin(char, astr[:midindex]) # otherwise test character larger middle character, recursively # search on last half of astr     else:         return isin(char, astr[midindex:])   astr = str(raw_input('enter word: ')) char = str(raw_input('enter character: ')) print(isin(char,astr)) 

looks never called function defined:

astr = raw_input('enter word: ')  #raw_input returns string ,no need of str   char = raw_input('enter character: ') print isin(char, astr)                  #call function run 

demo:

enter word: foo enter character: o true 

functions definition , execution:

the function definition not execute function body; gets executed when function called.

example:

def func():   #function definition, when parsed creates function object     return "you executed func"  print func()    #execute or run function executed func           #output 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -