python - Function returning 'None' -
i'm working on problem using python. here's concerned code problem i'm explain:
def no_to_words(n): num = str(n) s = "" if(len(num) == 3): hunds = n/100 ten = n%100 tens = ten/10 units = ten%10 if(n == 100): return "one hundred" if(hunds == 1): s = s + "one hundred and" elif(hunds == 2): s = s + "two hundred and" elif(hunds == 3): s = s + "three hundred and" elif(hunds == 4): s = s + "four hundred and" elif(hunds == 5): s = s + "five hundred and" elif(hunds == 6): s = s + "six hundred and" elif(hunds == 7): s = s + "seven hundred and" elif(hunds == 8): s = s + "eight hundred and" else: s = s + "nine hundred and" def final(t): ans = t return ans if(ten == 11): s = s + " eleven" final(s) print no_to_words(111)
now, function converts three-digit number it's alphabetic equivalent string(i haven't posted whole code here). now, if number '111' input, value of 'ten' 11. means, new value of 's' 'one hundred , eleven'. returning value , preventing program go further , check 'units' value(the code not included here), tried calling 'final' function, 's' parameter. , 'final' function returns value of 's'.
however, '111' input, 'none' output. what's wrong code?
although may right, don't return
every outcome.
although 111
fulfils condition @ end (111 / 10 == 11
, if ten == 11
) don't return value it.
to fix this, need do:
return final(s) # if don't return here, throwing away.
to return
branch. although @ moment calling final
returns value, in deeper scope (it return
caller). return overall function, need 'return returned value' if makes sense.
also, reason none
because when there no returned value, equivalent returning none
, output.
Comments
Post a Comment