471,350 Members | 1,585 Online

# Need some help...

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?

Thank you.....

Oct 28 '07 #1
10 1118 I want to create a program that I type in a word.

You can see that Python has a command to input strings from the
command line.
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?
Python has a dictionary data structure called dict(), or {}, that you
can use to map your letters to those numbers. With it you can created
the letter-number association.

Then you can scan the characters of the input string one after the
other, and sum their values into a single total value. Try writing
that code, and then show it to us, we can give more suggestions if you
need them...

Bye,
bearophile

Oct 28 '07 #2
On 10/28/07, hy********@gmail.com <hy********@gmail.comwrote:
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?

Will you put our names on your homework when you hand it in?

--

# p.d.
Oct 28 '07 #3
hy********@gmail.com a écrit :
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?
If the values are arbitrary:

letters_values = {'A':1, 'B':20, 'C':13, 'H':4, 'O':7, 'S':5, #etc...}
print letters_values['A']

Also, just curious, but, how could I then have the 3 and 0 added
together to be 3?
help(sum)
help(map)
help(int)
print int('3')
help(list)
print list('30')
help(str)
print str(30)
Oct 29 '07 #4
hy********@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?

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
Oct 30 '07 #5
Boris Borcic wrote:
hy********@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?

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

Oct 31 '07 #6
Ricardo Aráoz wrote:
Boris Borcic wrote:
>hy********@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?

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 ?
Nov 1 '07 #7
Boris Borcic wrote:
Ricardo Aráoz wrote:
>Boris Borcic wrote:
>>hy********@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?

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.

Nov 1 '07 #8
En Thu, 01 Nov 2007 20:12:52 -0300, Ricardo Aráoz <ri******@gmail.com>
escribió:
>>>>>def sumToOneDigit(num) :
if num < 10 :
return num
else :
return sumToOneDigit(sum(int(i) for i in str(num)))
def sumToOneDigit(num):
return num % 9 or 9

Valid when num>=1, which is guaranteed by the OP context.

--
Gabriel Genellina

Nov 2 '07 #9
Gabriel Genellina wrote:
En Thu, 01 Nov 2007 20:12:52 -0300, Ricardo Aráoz <ri******@gmail.com>
escribió:
>>>>>>def sumToOneDigit(num) :
if num < 10 :
return num
else :
return sumToOneDigit(sum(int(i) for i in str(num)))

def sumToOneDigit(num):
return num % 9 or 9

Valid when num>=1, which is guaranteed by the OP context.
Beautiful. Much better than mine.

Nov 2 '07 #10
Ricardo Aráoz wrote:
Boris Borcic wrote:
>Ricardo Aráoz wrote:
>>Boris Borcic wrote:
hy********@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?
>
>
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 :
Not me, the OP "hy********@gmail.com".
"""
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?)
I had provided a solution that conformed to the OP's 2 questions. Your
non-conform addition simply proved that you hadn't bothered to read.

Or else you meant to read the OP's mind beyond what he specified; but then you
should adress him, not me.
>
And answering the main question : sum(ord(ch) for ch in 'chaos'.upper())
which is inside the "sumToOneDigit" funtion in my answer.
No, you did not adress the original questions.

My "hth what ?" was an invitation to
rectify. Sorry it wasn't enough for you, but the verbosity was probably worth
what you paid for it.
>
Sorry if that is not enough for you, but the answer is probably worth
what you paid for it.
Nov 2 '07 #11

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 6 posts views Thread by mike | last post: by 5 posts views Thread by John Flynn | last post: by reply views Thread by xunling | last post: by 9 posts views Thread by sk | last post: by 7 posts views Thread by Timothy Shih | last post: by 15 posts views Thread by Cheryl Langdon | last post: by 16 posts views Thread by pamelafluente | last post: by 8 posts views Thread by skumar434 | last post: by reply views Thread by U S Contractors Offering Service A Non-profit | last post: by 20 posts views Thread by mike | last post: by reply views Thread by Yacine Si Tayeb | last post: by reply views Thread by Yacine Si Tayeb | last post: by reply views Thread by leo001 | last post: by reply views Thread by leo001 | last post: by reply views Thread by chloesad91 | last post: by reply views Thread by chloesad91 | last post: by reply views Thread by SwissProgrammer | last post: by reply views Thread by YTAIGamer | last post: by reply views Thread by shantun007 | last post: by