ord function problem from newbie

D

David.J.Anderson66

I'm trying to convert a name into a numerical value that is not
consistent with ANSCII values. In my case, I convert all to lowercase,
then try to sum the value of the letters entered by the user, can't
get it to add them. Here is what I have. By the way, the values I need
to use is: a=1, b=2, c=3, etc... I'm trying to subtract 96 from the
ANSCII value, then total.

import string
def main():
print "This program calculates the numeric value of a name with
which"
print "you could look up online as to what that value represents."
print
# Get name to calculate
name = raw_input("Please type a name: ")
small = string.lower(name)
print "Here is the calculated value:"

print small
for ch in small:
v = ord(ch)-96
print v


main()

Looks like this:
This program calculates the numeric value of a name with which
you could look up online as to what that value represents.

Please type a name: David
Here is the calculated value:
david
4
1
22
9
4
 
P

Paul Rubin

for ch in small:
v = ord(ch)-96
print v


total = 0
for ch in small:
# the += below updates the value of total by adding (ord(ch) - 96)
total += (ord(ch) - 96)
print "ch:", ch, "total so far:", total
 
7

7stud

I convert all to lowercase,

import string

small = string.lower(name)
print "Here is the calculated value:"

print small
for ch in small:
v = ord(ch)-96
print v

I don't know if you are using an out of date book or what, but
generally you are not going to be importing string. For example,

my_str = "HELLO"
small = my_str.lower()
print small

--output:--
hello
 

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,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top