# Recursive function that prints digits of a number in english

Page 1 of 1

## 2 Replies - 3302 Views - Last Post: 08 December 2011 - 11:18 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=258934&amp;s=fe06851864b087a28a8ca3eff99ea654&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 python12345

Reputation: 0
• Posts: 1
• Joined: 07-December 11

# Recursive function that prints digits of a number in english

Posted 07 December 2011 - 03:49 PM

I'm trying to write a program that uses a recursive function to print the digits of a number in English (i.e. 456 would display "Four Five Six".) I would also like the program to obtain multiple numbers from a user and display the corresponding English digits.

I started by obtaining a number from the user and running through a sentinel loop until the user presses <Enter> to end the loop. I then appended each number to a list; however, I'm not sure if that is the proper way to go about completing this task.

Regardless, when I created my functions to get the user input I am running into some errors and cannot figure out what the problem is.

```def main():
List = createList()

def getNumber(prompt):
#Loop while the value entered is not a valid number
try:
number = eval(input(prompt))
if type(number) == type(0) or type(number) == type(0.0):
else:
print("\nYou did not enter a number. Try again.")
except NameError:
print("\nYou did not enter a number. Try again.")
except SyntaxError:
print("\nYou did not enter a number. Try again.")
except:
print("\nAn exception occured. Try again.")
if number == "":
return None
else:
return number

def createList(prompt):
#Create a blank list
newList = []
item = getNumber("Enter a list of numbers (<Enter> to quit): ")
while item != None:
#Add user input to the end of the created list
newList.append(item)
item = getNumber("Enter a list of numbers (<Enter> to quit): ")
return newList

def displayEnglishDigits(number):
numEnglish = {0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four",
5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"}
digit = numEnglish[Number % 10]
return displayEnglishDigits(number//10) + digit

main()
```

At this point I am stuck and unsure where to go. Any help would be appreciated.

Is This A Good Question/Topic? 0

## Replies To: Recursive function that prints digits of a number in english

### #2 Tayacan

• D.I.C Regular

Reputation: 145
• Posts: 275
• Joined: 18-January 11

## Re: Recursive function that prints digits of a number in english

Posted 08 December 2011 - 03:30 AM

Could you post the error message?

### #3 cupidvogel

Reputation: 31
• Posts: 593
• Joined: 25-November 10

## Re: Recursive function that prints digits of a number in english

Posted 08 December 2011 - 11:18 PM

Firstly this is no recursive function. You are inputting a number with one digit per prompt, that takes the "number" feeling out of it. You need to enter the number in one go and assign it to some variable:

```num = int(raw_input("Enter the number: "))

```

Once you have got the number, then you can extract its digits by using a recursive function (or you can do it with a simple loop without need for recursion), and then matching them against the dictionary you can print out the corresponding words...