Need some help...

H

hyozan.ux3

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.....
 
B

bearophileHUGS

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
 
M

martyw

You can see that Python has a command to input strings from the
command line.


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
as an alternative for the suggest dictionary approach you could study
the built in functions ord() and chr(), see the documentation
(http://docs.python.org/lib/built-in-funcs.html)
 
P

Peter Decker

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.

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

Bruno Desthuilliers

(e-mail address removed) 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)
 
B

Boris Borcic

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
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

Boris said:
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

if num < 10 :
return num
else :
return sumToOneDigit(sum(int(i) for i in str(num)))

6



HTH
 
B

Boris Borcic

Ricardo said:
Boris said:
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)))

6



HTH

HTH what ?
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

Boris said:
Ricardo said:
Boris said:
(e-mail address removed) 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.
 
G

Gabriel Genellina

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

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

=?ISO-8859-15?Q?Ricardo_Ar=E1oz?=

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

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

Beautiful. Much better than mine.
 
B

Boris Borcic

Ricardo said:
>>> Boris Borcic wrote:
>>>> (e-mail address removed) 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 :

Not me, the OP "(e-mail address removed)".
> """
> 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top