1 Replies - 504 Views - Last Post: 08 December 2011 - 11:26 PM Rate Topic: -----

#1 benmiesner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-November 11

Recursive Function Numbers to English

Posted 06 December 2011 - 07:04 PM

Write a recursive function to print the digits of a number in English. For example, the number 153's output should be "One Five Three"
a.Name your function displayEnglishDigits(number).
b. Obtain numbers from the user and display the English digits for each number entered. You may terminate the loop based on a sentinel value or by first asking the user how many numbers will
be entered.
def getNumbers():
    n = []
    xStr = input("Enter first digit of number (negative to quit) >> ")
    integer = int(xStr)
    while  integer >= 0:
        while xStr != "":
            x = eval(xStr)
            n.append(x)
            xStr = input("Enter next digit of number (negative to quit) >> ")
    return n

def displayEnglishDigits(number):
    number = getNumbers()
    if len(number) == 0:
        return None
    if len(number) == 1:
        return number[0]
    else:
        value = displayEnglishDigits(number[1:])
        return value

def display(values):
    print(displayEnglishDigits(number))

def main():
    numb = getNumbers()
    nums = displayEnglishDigits(numb)
    display(nums)

main()



Is This A Good Question/Topic? 0
  • +

Replies To: Recursive Function Numbers to English

#2 cupidvogel  Icon User is offline

  • D.I.C Addict

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

Re: Recursive Function Numbers to English

Posted 08 December 2011 - 11:26 PM

Strange, I saw a near similar question by some other user in this section, with the same problem in his code! Firstly there is no recursion here. Recursive function is a function that continuously calls itself with a set of parameters revised during its current iteration. There is no such function here. You are using loop to accept a number??? Why, just assign the input number to a variable, check for abnormalities (whether it contains an a non-digit), and then extract its digits using a recursive function (or just a plain while/for loop), match them against a list containing one, two, three,...nine etc and print them out!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1