Problem with a dictionary program....

Ling Lee janimal at mail.trillegaarden.dk
Wed Sep 29 00:12:30 CEST 2004


Thanks for all your help, I really appreciate it, its very interesting to 
start programming but at times a bit confusing :D
I read all your input, and clap my hands for this friendly newsgroup :D

Stay safe....


"Larry Bates" <lbates at syscononline.com> wrote in message 
news:ZJadnSMpP7XiCsTcRVn-oA at comcast.com...
> Ling Lee wrote:
>> Hello.
>>
>> I'm trying to write a small program that lets you put in a number as an 
>> integer and then it tells you the textuel representation of the number.
>>
>> Like if your input is 42, it will say four two.
>>
>> I found out that I have to make a dictionary like this: List = { 1:"one", 
>> 2:"two" and so on )
>>
>> and I have to use the raw_input method to get the number:
>>
>> indput : raw_input(" Tell me the number you want to transform to textuel 
>> representaion")
>>
>> The I have to transform the input to a string
>> indput = str(indput)
>>
>> so that I can count how many decimals the number has, like 23 has 2 
>> decimals and 3000 has 4 decimals.
>>
>> After I have gotten the lenght of the string, I will write a loop, that 
>> goes through the dictionary as many times as the lengt of the string, and 
>> the gives me the corresponding numbers, the numner 21 would go 2 times 
>> through the loop and give me the output two one
>>
>> Will one of you be so kind and tell me how I count the lengt of the 
>> indput number i was thinking on something like input.count[:] but that 
>> dosnt work...
>>
>>  and how I make the loop.
>>
>>  Im trying to understand dictionaries but have gotten a bit stuck...
>>
>> Thanks for all replies....
>>
>>
>>
> Here's an example of what you want:
>
> """ Just run it, you'll see what it does.
>
> This code is released into the public domain absolutely free by 
> http://journyx.com
> as long as you keep this comment on the document and all derivatives of 
> it.
>
> """
>
> def getfractionwords(num):
>         frac = num-int(num)
>         numstr = str(int(frac*100+.5))
>         if len(numstr) == 1:
>                 numstr = '0'+numstr
>         fracstr = ' and ' + numstr + '/100'
>         return fracstr
>
> def convertDigit(digit):
>         digits = ('', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 
> 'Seven', 'Eight', 'Nine')
>         return digits[int(digit+.5)]
>
> def breakintochunks(num):
>         (left,right) = breakintwo(num)
>         rv = [right]
>         while left > 999:
>                 (left,right) = breakintwo(left)
>                 rv.append(right)
>         rv.append(left)
>         rv.reverse()
>         return rv
>
> def breakintwo(num):
>         leftpart = int(num/1000.0)+0.0
>         rightpart = 1000.0*(num/1000.0 - leftpart)
>         return (int(leftpart+.5),int(rightpart+.5))
>
> def enties(num):
>         tens = ('','','Twenty' ,'Thirty', 'Forty', 'Fifty', 'Sixty',
>                 'Seventy', 'Eighty', 'Ninety')
>         indx = int(num/10.0)
>         return tens[indx]
>
> def convert2digit(num):
>         teens = ('Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen',
>                  'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 
> 'Nineteen')
>         if num < 10:
>                 return convertDigit(num)
>         if num <20:
>                 return teens[num-10]
>         if num > 19:
>                 tens = enties(num)
>                 ones = convertDigit(10*(num/10.0-int(num/10.0)))
>                 if ones:
>                         rv= tens+'-'+ones
>                 else:
>                         rv=tens
>                 return rv
>
> def convert3digit(num):
>         threenum = str(num)
>         ln = len(threenum)
>         if ln==3 :
>                 a= convertDigit(int(threenum[0]))
>                 b= ' Hundred '
>                 c=     convert2digit(int(threenum[1:]))
>                 return a+b+c
>         if ln<3 :
>                 return convert2digit(int(threenum))
>         raise 'bad num',num
>
> def num2words(num):
>         thousandchunks = breakintochunks(int(num))
>         rv = ' '
>         if num >= 1000000:
>                 rv=rv+ convert3digit(thousandchunks[-3])+ ' Million '
>         if num >= 1000:
>                 c3d= convert3digit(thousandchunks[-2])
>                 if c3d:
>                         rv=rv+ c3d+ ' Thousand '
>         rv = rv + convert3digit(thousandchunks[-1])+ getfractionwords(num)
>         return squishWhiteSpace(rv)
>
> def squishWhiteSpace(strng):
>         """ Turn 2 spaces into one, and get rid of leading and trailing
>             spaces. """
>         import string,re
>         return string.strip(re.sub('[ \t\n]+', ' ', strng))
>
> def main():
>         for i in range(1,111,7):
>                 print i,num2words(i)
>
>         for i in (494.15, 414.90, 499.35, 400.98, 101.65, 110.94, \
>                   139.85, 12349133.40, 2309033.75, 390313.41, 99390313.15, 
> \
>                   14908.05, 10008.49, 100008.00, 1000008.00, 100000008.00, 
> \
>                   14900.05, 10000.49, 100000.00, 1000000.00, 100000000.00, 
> 8.49):
>                 print i,num2words(i)
>
>         import whrandom
>         for i in range(33):
>                 num = whrandom.randint(1,999999999) + 
> whrandom.randint(1,99)/100.0
>                 print num,num2words(num)
>
> if __name__ == '__main__':
>         main() 





More information about the Python-list mailing list