def break_up_number(n): """ Break up a number n into three triplets representing millions, thousands, and hundreds and return the triplet values. """ millions = n//1_000_000 remainder = n%1_000_000 thousands = remainder//1000 hundreds = remainder%1000 return millions, thousands, hundreds def break_up_triplet(n): """ Break up a triplet value n into the hundreds, tens, and ones digits and return the digit values. """ hundreds_digit = n//100 remainder = n%100 tens_digit = remainder//10 ones_digit = remainder%10 return hundreds_digit, tens_digit, ones_digit def translate_ones_digit(digit): """ Return the translated ones digit. """ if digit == 1: return 'one' elif digit == 2: return 'two' elif digit == 3: return 'three' elif digit == 4: return 'four' elif digit == 5: return 'five' elif digit == 6: return 'six' elif digit == 7: return 'seven' elif digit == 8: return 'eight' else: return 'nine' def translate_teens(digit): """ Return the translated teens digit. """ if digit == 0: return 'ten' elif digit == 1: return 'eleven' elif digit == 2: return 'twelve' elif digit == 3: return 'thirteen' elif digit == 4: return 'fourteen' elif digit == 5: return 'fifteen' elif digit == 6: return 'sixtern' elif digit == 7: return 'seventeen' elif digit == 8: return 'eighteen' elif digit == 9: return 'nineteen' def translate_tens_digit(digit): """ Return the translated tens digit. """ if digit == 2: return 'twenty' elif digit == 3: return 'thirty' elif digit == 4: return 'forty' elif digit == 5: return 'fifty' elif digit == 6: return 'sixty' elif digit == 7: return 'seventy' elif digit == 8: return 'eighty' else: return 'ninety' def translate_pair(tens_digit, ones_digit): """ Return the translation of a pair digits: a tens digit and a ones digit. """ if (tens_digit == 0) and (ones_digit > 0): str = translate_ones_digit(ones_digit) elif tens_digit == 1: str = translate_teens(ones_digit) else: str = translate_tens_digit(tens_digit) # Append a hyphen and the ones word. if ones_digit > 0: str += '-' + translate_ones_digit(ones_digit) return str def translate_triplet(triplet): """ Return the translation of a triplet. """ str = ''; hundreds_digit, tens_digit, ones_digit = break_up_triplet(triplet) if hundreds_digit > 0: str = translate_ones_digit(hundreds_digit) + ' hundred' # Append a space only if there is something else. if (tens_digit > 0) or (ones_digit > 0): str += ' ' # If there is a pair, append their translation. if (tens_digit != 0) or (ones_digit != 0): str += translate_pair(tens_digit, ones_digit) return str def translate(n): """ Return the translation of a number. """ str = ''; millions, thousands, hundreds = break_up_number(n) if millions > 0: str += translate_triplet(millions) + ' million' if thousands > 0: if millions > 0: str += ' ' str += translate_triplet(thousands) + ' thousand' if hundreds > 0: if (millions > 0) or (thousands > 0): str += ' ' str += translate_triplet(hundreds) return str; def prompt_for_number(): """ Prompt the user for a number and return its value. """ good_number = False while not good_number: number = int(input('Number to translate? ')) good_number = (0 <= number <= 999_999_999) if not good_number: print('Number must be between 1 and 999,999,999 inclusive. 0 to stop.') return number n = prompt_for_number() # Loop to prompt for and translate numbers # until the user enters 0. while n > 0: print(translate(n)) print() n = prompt_for_number()