# Python Converting numbers to words, using Recursive

My function recursively calls itself until it decodes every numbers. rec_ntw

face_value_count -> (Incremental variable) denotes ones, tens, hundreds and thousands face_value -> (Dictionary) denotes the dictionary containing corresponding the word value for numbers.

result -> (String) Contains final result to return.

teen -> (Integer) to decode the teen numbers from eleven to ninteen

number % 10 to get the last element

number // 10 to remove the last element and pass it on to recursive the function.

def rec_ntw(number, face_value_count=0):
result = ""
teen = 0
if number <= 0:
if face_value_count == 0:
result = "Zero"
print (result)
#return result
else:
if face_value_count == 0:
result = (singleton[int(number % 10)])
teen = number % 10
elif face_value_count == 1:
if number % 10 == 1:
result = (face_value[1][1][int(teen)]) + " "
else:
result = (face_value[int(face_value_count)][int(number % 10)]) + " " + result
teen = 0
elif face_value_count == 2:
if number % 10 != 0:
result = (singleton[int(number % 10)] + " " + str(face_value[int(face_value_count)]) + " ") + result
else:
result = (singleton[int(number % 10)] + " " + str(face_value[int(face_value_count)]) + " ") + result
face_value_count += 1
rec_ntw(number // 10, face_value_count)


Please review my code and lemme know how to write effectively.

• seeing the definition of face_value would be a great addition to this post. – Mathias Ettinger Mar 16 '18 at 15:18
• what is singleton? – Maarten Fabré Mar 16 '18 at 15:21

What is your desired output? Your function has no return statements (other the one commented out), and the only print statement is in the number <= 0 block. Have you run this function? Unless I'm missing something, it won't do anything if you input 0. And when you strip out the last digit, you don't actually store it anywhere, so it's just gone.

You seem to be doing double tabs for your indents, which makes it hard to read once you get several levels deep. Also, you can save yourself an indent level after the number <= 0 block; since you have nothing but an else block after this, you can put a return at the end of the if block. Then you won't ever get to the else block unless the if condition is false, making the else redundant. So you can get rid of that and move everything in an indent level.

You don't show what face_value is, so it's hard to evaluate your code when it comes to that part.

Also, unless I'm misunderstanding, the if number <= 0 block should be something like this:

if face_value_count == 0:
result = "Zero"
print (result)
return(result)
return('negative '+rec_ntw(-number, face_value_count))


You could replace the if else blocks after the number <= 0 block with a case statement.

Using recursion in this case is rather odd. The basic structure of your function is:

def f(x):
#do action 1
#do action 2
#do action 3


But you're implementing it as:

def f(x,instruction_address):
# do action 1
# do action 2
# do action 3


That's just ... really weird.

Consider the following code:

def numbers_to_words(number):
numbers = ['zero','one','two','three','four','five','six','seven','eight','nine',
'ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen',
'seventeen','eighteen','nineteen']
tens_places = ['twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety']
if number <20:
return(numbers[number])
#print(number)
if number%10 == 0:
return(tens_places[number//10-2])
return(tens_places[int(str(number)[0])-2]+'-'+numbers[int(str(number)[1])])


This turns any number from 0 to 99 to words, no recursion. It can easily be extended to -999 to 999, and a little more work would get it to, say, -10^33 to 10^33.