2 Replies - 1467 Views - Last Post: 08 December 2011 - 11:18 PM Rate Topic: -----

#1 python12345  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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):
    badNumber = True
    #Loop while the value entered is not a valid number
    while badNumber:
        try:
            number = eval(input(prompt))
            if type(number) == type(0) or type(number) == type(0.0):
                badNumber = False
            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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1