[Tutor] please return flys in ointment
Jim Mooney
cybervigilante at gmail.com
Sun Jul 7 04:40:15 CEST 2013
On 6 July 2013 19:09, Dave Angel <davea at davea.name> wrote:
>
> If the exception was on the line:
Actually, it was on the line:
if number_name[-2] == ',':
number_name = number_name[0:-2] # special case - snip
extraneous ending comma
And only intermittently. It probably occurred in a huge, oddly-formed
number, larger than your test, since I'm using randint to give
random-numbers of random size for testing. I did that since actually
iterating up to the decillions is a bit beyond my modest computer ;')
> If you'd like, I'll post my full version, changed as little as possible from
That would be helpful. The corrections are already four times the size
of the program, and at least three pots of coffee ;')
Since I didn't make it a full function yet, just user-input, I
abstracted the main func into the test program below, which finds the
error between 5,000 and 9,000 tests. The offending line was an easy
cheat but didn't work, alas ;')
Abstracting it was annoying so I can see where it would be better to
make it all into a command line function. And maybe a class later,
since I want to add to it a bit.
import sys
from random import randint, seed
seed()
# Data
ones = {'1': 'one', '2': 'two', '3': 'three', '4': 'four', '5':
'five', '6': 'six',
'7': 'seven', '8': 'eight', '9': 'nine'}
tens = {'2': 'twenty', '3': 'thirty', '4': 'forty', '5': 'fifty', '6': 'sixty',
'7': 'seventy', '8': 'eighty', '9': 'ninety'}
doubles = {'0': 'ten', '1': 'eleven', '2': 'twelve', '3': 'thirteen',
'4': 'fourteen',
'5': 'fifteen', '6': 'sixteen', '7': 'seventeen', '8': 'eighteen',
'9': 'nineteen'}
powers_of_1000 = (' thousand', ' million', ' billion', ' trillion',
' quadrillion', ' quintillion', ' sextillion', ' septillion', ' octillion',
' nonillion', ' decillion')
def numbers_to_name(triplets, triplen):
'''Create a name from each triplet and append the power of ten'''
triplen -= 2
number_name = ''
for triplet in triplets:
triplet_name = ''
first, second, third, last_two = (triplet[0], triplet[1],
triplet[2], triplet[1:])
if triplet == '000':
triplen -= 1
continue
if first > '0':
if last_two == '00': # special case - snip extra space separator
triplet_name += ones.get(first) + ' hundred'
else:
triplet_name += ones.get(first) + ' hundred '
if second == '0':
if third > '0':
triplet_name += ones.get(third)
elif second == '1':
triplet_name += doubles.get(third)
elif second > '1':
triplet_name += tens.get(second)
if third > '0':
triplet_name += '-' + ones.get(third)
number_name += triplet_name
if triplen > -1:
number_name += powers_of_1000[triplen] + ', '
triplen -= 1
if number_name[-2] == ',':
number_name = number_name[0:-2]
# special case - snip extraneous ending comma
return number_name
def testfunc():
triplets = []
length_of_triplets = randint(1,11)
for cnt in range(0, length_of_triplets):
triplet = ''
triplet += str(randint(0,9))
triplet += str(randint(0,9))
triplet += str(randint(0,9))
triplets.append(triplet)
return triplets, length_of_triplets
with open('triptest.txt', 'w') as triptest:
for x in range(9000):
triplets, triplen = testfunc()
number_name = numbers_to_name(triplets, triplen)
printout = str(triplets) + '\n' + number_name + '\n\n'
triptest.write(printout)
--
Jim
"I asked God for a bike, but I know God doesn't work that way. So I
stole a bike and asked for forgiveness." ~ Anonymous
More information about the Tutor
mailing list