[Tutor] unwanted 'zero' ending
Peter Otten
__peter__ at web.de
Thu Jun 27 09:19:32 CEST 2013
Jim Mooney wrote:
> I've been staring at this program for hours, which otherwise seems to
> work fine - converting numbers to their words - but the one glaring
> error is that final hundreds have a 'zero' appended, such as hundred
> zero, two hundred zero, (See the very end of the test output, which
> ends in four hundred zero, instead of four hundred, as it should.) I'm
> sure this is right in front of me but I must be getting fuzzy ;')
I'd try skipping (i. e. not producing any output for) "000" triplets. That
should (untested) reduce the number of incorrect results to a single one.
Can you guess which one I have in mind?
If my assumption is correct you can easily deal with that value by special-
casing.
> By the way, how large is Python's standard GUI input allowed to be,
> and can you expand it without resorting to tkinter?
>
> #Using C:\Python33\python.exe on Win 7 in c:\python33\jimprogs
> def numstonames(inp):
> ''' Return names for positive integer input, up to 10**36-1. You
> can separate input
> with spaces (not commas) to avoid confusion.
> '''
> multipliers = ('thousand', 'million', 'billion', 'trillion',
> 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion',
> 'nontillion', 'dectillion','')
>
> singles = {'1': 'one', '2': 'two', '3': 'three', '4': 'four', '5':
> 'five', '6': 'six',
> '7': 'seven', '8': 'eight', '9': 'nine'}
>
> lownums = {'00': 'zero', '01': 'one', '02': 'two', '03': 'three',
> '04': 'four',
> '05': 'five', '06':'six', '07': 'seven', '08': 'eight', '09':
> 'nine','10': 'ten',
> '11': 'eleven', '12': 'twelve', '13': 'thirteen', '14':
> 'fourteen', '15': 'fifteen',
> '16': 'sixteen', '17': 'seventeen', '18': 'eighteen', '19':
> 'nineteen'}
>
> twenty_to_90 = {'2': 'twenty', '3': 'thirty', '4': 'forty', '5':
> 'fifty', '6': 'sixty', '7': 'seventy', '8': 'eighty', '9': 'ninety'}
>
> def twenty_to_99(inp):
> ''' Return name for numbers from 20 to 99'''
> last_two = twenty_to_90.get(inp[0])
> if inp[1] != '0': last_two += '-' + singles.get(inp[1])
> return last_two
>
> inp = str(inp)
> inlen = len(inp)
> triplet_name = ''
> first_digit = ''
> last_two = ''
> numword = ''
>
> # left-pad input with zeros so it's a multiple of 3, and we get
> all triplets.
> padnum = (3 - inlen % 3)
> if padnum != 3:
> inp = inp.zfill(padnum + inlen)
>
> # Break input into triplets
> triplets = [inp[i:i+3] for i in range(0,len(inp),3)]
> get_multiplier = len(triplets) - 2
> for triplet in triplets:
> last_two = lownums.get(triplet[1:]) # Get last two numwords in
> triplet, if 0 to 19
> if last_two == None: last_two = twenty_to_99(triplet[1:]) # or
> get larger numword
>
> # Get first digit of triplet, if non-zero, which will be in the
> # hundreds
> if triplet[0] != '0': first_digit = singles.get(triplet[0]) +
> ' hundred '
> triplet_name = first_digit + last_two
> numword += triplet_name + ' '# concatenate the triplets
>
> # Append the proper multiplier: thousands, millions, billions,
> # etc.
> numword += multipliers[get_multiplier] + ' '
> get_multiplier -= 1
>
> return numword
>
> number_candidate = input("input positive integer to convert, space
> separated or not")
> numlist = number_candidate.split()
> actual_number = ''.join(numlist)
> print(numstonames(actual_number))
>
> # Test input: 123 456 700 543 928 103 953 262 950 681 161 400
> '''Output:
> one hundred twenty-three dectillion four hundred fifty-six nontillion
> seven hundred zero octillion five hundred forty-three septillion
> nine hundred twenty-eight sextillion one hundred three quintillion
> nine hundred fifty-three quadrillion two hundred sixty-two trillion
> nine hundred fifty billion six hundred eighty-one million
> one hundred sixty-one thousand four hundred zero '''
>
More information about the Tutor
mailing list