Need some help...
Ricardo Aráoz
ricaraoz at gmail.com
Fri Nov 2 00:12:52 CET 2007
Boris Borcic wrote:
> Ricardo Aráoz wrote:
>> Boris Borcic wrote:
>>> hyozan.ux3 at gmail.com wrote:
>>>> I want to create a program that I type in a word.
>>>>
>>>> for example...
>>>>
>>>> chaos
>>>>
>>>> each letter equals a number....
>>>>
>>>> A=1
>>>> B=20
>>>>
>>>> and so on.
>>>>
>>>> So Chaos would be
>>>>
>>>> C=13 H=4 A=1 O=7 S=5
>>>>
>>>> I want to then have those numbers
>>>> 13+4+1+7+5 added together to be 30.
>>>>
>>>> How can I do that?
>>>>
>>>> Also, just curious, but, how could I then have the 3 and 0 added
>>>> together to be 3?
>>>>
>>>> Please help me out.
>>>>
>>>> Thank you.....
>>>>
>>> >>> sum(dict(C=13,H=4,A=1,O=7,S=5)[_] for _ in 'CHAOS')
>>> 30
>>> >>> sum(eval(ch) for ch in str(_))
>>> 3
>>
>>>>> def sumToOneDigit(num) :
>> if num < 10 :
>> return num
>> else :
>> return sumToOneDigit(sum(int(i) for i in str(num)))
>>
>>
>>>>> sumToOneDigit(sum(ord(ch) for ch in 'chaos'.upper()))
>> 6
>>
>>
>>
>> HTH
>
> HTH what ?
HTH : Hope that helps
Citing your post :
"""
Also, just curious, but, how could I then have the 3 and 0 added
together to be 3?
"""
you have the function "sumToOneDigit" that adds the digits of a number
till you get one digit (isn't that what you where curious about?)
And answering the main question : sum(ord(ch) for ch in 'chaos'.upper())
which is inside the "sumToOneDigit" funtion in my answer.
Sorry if that is not enough for you, but the answer is probably worth
what you paid for it.
More information about the Python-list
mailing list