how do I factor a number down to one digit?

A

Allan

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

bearophileHUGS

Allan>I hope this isn't too stupid of a question.

It's a simple problem, but it's not a stupid question, this is a
possible solution:

data = "myname"
radix = str(sum(ord(c)-96 for c in data))
while len(radix) > 1:
radix = str(sum(int(c) for c in radix))
print "The radix of:\n", data, "\n\nIs:\n", radix

Bye,
bearophile
 
S

Steven D'Aprano

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

bearophileHUGS

Sounds like homework to me.

Sorry Steven, you may be right, next time I'll be more careful.

Bye,
bearophile
 
J

johnzenger

Your tools are:

1. "map" lets you apply a function to every element of a list.
Strings are lists. (List comprehensions let you do the same thing, but
"map" is better to use if you are turning in homework).
2. "sum" lets you calculate the sum of all numbers in a list.
3. "val" and "str" let you turn strings into numbers and numbers into
strings

The only missing piece is a function that turns a letter into a number.
It shouldn't be that hard to write one.
 
T

Tim Roberts

Allan said:
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.

Hmm, in most of the rational mathematical universes I've visited, 1+2+3
returns 6, not 5.

On the other hand, numerology doesn't really have much of a place in a
rational mathematical universe.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top