how do I factor a number down to one digit?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Feb 27 06:18:12 EST 2006


On Mon, 27 Feb 2006 02:31:42 -0800, Allan wrote:

> I'm trying to write a numerology program where I have each letter
> identified by a numerical value like
> a=1
> b=2
> c=3
> as so forth. I then input a name. How do I treat each letter as a
> single value? That is, instead of print myname I have to do a print
> m+y+n+a+m+e which returns a number. I next want to convert the
> resulting two or three digit number to a single digit. Like 123 would
> be 1+2+3 returning a 5. I hope this isn't too stupid of a question.

Sounds like homework to me.

Here is a hint:

myname = "steven"
for c in myname:
    do_something_with(c)

does some work with each individual letter of myname.

You want something that takes 'a', and gives 1, 'b' which gives 2, 'c'
which gives 3, and so forth. In other words, you want to map letters of
the alphabet to numbers. Hint: experiment with dictionaries.

sum = 0
myname = "steven"
for c in myname:
    sum = sum + map_letter_to_number_somehow

That's the first half of the problem. Chew on that for a while, and if you
still need help, come back and ask.


-- 
Steven.




More information about the Python-list mailing list