[Tutor] Problem with variables

Karl Pflästerer sigurd at 12move.de
Wed Nov 5 13:37:08 EST 2003


On 24 Oct 2003, docnova <- docnova at charter.net wrote:

> There are several problems that I need to figure out how to address. The first
> (and biggest) is the inuput of the users name. In numerology the one of the
> core numbers is acheived by adding each letter of the users full name
> together. As of now the only solution is for the user to enter the name as
> such "J+O+H+N+S+M+I+T+H".

> The ideal scenario would be for "John Smith" to be translated into
> "J+O+H+N+S+M+I+T+H"

> How would I make this action possible?


> Here is the source i have so far:

> #This program will give a complete numerology rating :)
> #Each letter represents a number. Each letter is given its proper value.
> a=1;  A=1
> b=2;  B=2
> c=3; C=3
[...]
> y=1; Y=1
> z=7; Z=7

Remeber: characters in Python have to be enclosed in either single or
double quotes.

> print "Welcome"
> print "This program will give you a complete numerology rating :)"
> name = raw_input("Please enter your full name (first, middle, last): ")
> print "Hello there", name +"."

For all these tasks where you have to do the same action again and again
it can be good to work with a list.

So first you'd have to transform the string from raw_input into a list.
That's easy: simply use the function `list'.

Then you'd have to walk through that list and find the the value for
each character and finally sum these values up.

To have a mapping between your characters and their values you could use
a dictionary.  That's a special kind of table (hash table) where you can
use eg. characters as keys to store and later retrieve values.

So let's start (the following solution is only a raw sketch but should
give you a hint how to solve your problem).



table = {'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5, 'f' : 8, 'g' : 3,
         'h' : 5, 'i' : 1, 'j' : 1, 'k' : 2, 'l' : 3, 'm' : 4, 'n' : 5,
         'o' : 7, 'p' : 8, 'q' : 1, 'r' : 2, 's' : 3, 't' : 4, 'u' : 6,
         'v' : 6, 'w' : 6, 'x' : 6, 'y' : 1, 'z' : 7}

# here we have the dictionary; as the case doesn't matter we only use
# lower case letters

print "Welcome"
print "This program will give you a complete numerology rating :)"
name = raw_input("Please enter your full name (first, middle, last): ")
print "Hello there", name +"."

# here we convert our string to a list of chars

name = list(name)

# now we iterate through the list and sum up the values; we convert each
# char to lowercase

# the following can (and should IMO) be written as a function.  But as we
# use it here only once and it's very simple it's also ok like that.
# We use here try ... except because the spaces are also converted to list
# members

summ = 0
for char in name:
    try:
        summ += table[char.lower()]
    except KeyError:
        pass


print sum



   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list