473,387 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

numeric value of a single name.

I'm having trouble writing a program that calculates the numeric value of a single name. . .

ie: a = 1, b = 2, and so on.

I have to use the ASCII character set and ord function. And I have to force the upper/lower case.

Anyone? I'm new to Python and I'm trying to learn it. :(
Mar 1 '07 #1
17 11070
bvdet
2,851 Expert Mod 2GB
I'm having trouble writing a program that calculates the numeric value of a single name. . .

ie: a = 1, b = 2, and so on.

I have to use the ASCII character set and ord function. And I have to force the upper/lower case.

Anyone? I'm new to Python and I'm trying to learn it. :(
I do not understand your problem. I doubt this is what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. >>> def char_value(character):
  2. ...     return ord(character)-96
  3. ... 
  4. >>> char_value('A')
  5. -31
  6. >>> char_value('a')
  7. 1
  8. >>> char_value('b')
  9. 2
  10. >>> 
Perhaps you could post some of your own code to give us a better understanding of what you are trying to accomplish.
Mar 1 '07 #2
That could help. . . Right now, all I have is (the beginning of it).

def main():

print "Numeric value for a single name"
name = raw_input("Please enter your first name: ")


I've been trying to write this program for a while, but I've been stuck. I'm very new at this.
Mar 1 '07 #3
bvdet
2,851 Expert Mod 2GB
That could help. . . Right now, all I have is (the beginning of it).

def main():

print "Numeric value for a single name"
name = raw_input("Please enter your first name: ")


I've been trying to write this program for a while, but I've been stuck. I'm very new at this.
Maybe this will help:
Expand|Select|Wrap|Line Numbers
  1. >>> def char_value(character):
  2. ...     return ord(character)-96
  3. ... 
  4. >>> sum = 0
  5. >>> name = 'Bill'
  6. >>> for letter in name:
  7. ...     sum += char_value(letter)
  8. ...     
  9. >>> sum
  10. 3
  11. >>> 
Mar 1 '07 #4
Hmm, I messed up somewhere else and it wouldn't even let me run it. So I'm going to stop for tonight. :(

What I'm trying to do with this program is have a name entered, add up the value, and come up with a sum.

example: Zelle
26 + 5 + 12 + 12 + 5 = 60

It sounds like an somewhat easy program (since this would be my fourth one), but after staring at it for a few hours, it's not that easy anymore.
Mar 1 '07 #5
ghostdog74
511 Expert 256MB
Hmm, I messed up somewhere else and it wouldn't even let me run it. So I'm going to stop for tonight. :(

What I'm trying to do with this program is have a name entered, add up the value, and come up with a sum.

example: Zelle
26 + 5 + 12 + 12 + 5 = 60

It sounds like an somewhat easy program (since this would be my fourth one), but after staring at it for a few hours, it's not that easy anymore.
seems like you are just substituting alphabet values with numbers, irregardless of caps or small letters. so i guess for this case, you can map each letter to a number, since there are only 26 of them
Expand|Select|Wrap|Line Numbers
  1.  
  2. alpha2num = { 'a' : 1, 'b' : 2, #and so on}
  3. print "Numeric value for a single name"
  4. name = raw_input("Please enter your first name: ").lower()
  5. total = 0
  6. for ch in name:
  7.    total = total + alpha2num[ch]
  8.  
  9. print total
  10.  
  11.  
Mar 1 '07 #6
dshimer
136 Expert 100+
If you use the ord() examples, you can force the case before the letters are evaluated using either upper() or lower().

Expand|Select|Wrap|Line Numbers
  1. >>> Astring='SomeLetters'
  2. >>> Astring.upper()
  3. 'SOMELETTERS'
  4. >>> Astring.lower()
  5. 'someletters'
Mar 1 '07 #7
seems like you are just substituting alphabet values with numbers, irregardless of caps or small letters. so i guess for this case, you can map each letter to a number, since there are only 26 of them
Expand|Select|Wrap|Line Numbers
  1.  
  2. alpha2num = { 'a' : 1, 'b' : 2, #and so on}
  3. print "Numeric value for a single name"
  4. name = raw_input("Please enter your first name: ").lower()
  5. total = 0
  6. for ch in name:
  7.    total = total + alpha2num[ch]
  8.  
  9. print total
  10.  
  11.  


I've tried the 'a' : 1 before, but for some odd reason python keeps saying it's wrong.
Mar 1 '07 #8
ghostdog74
511 Expert 256MB
I've tried the 'a' : 1 before, but for some odd reason python keeps saying it's wrong.
remove the comment #and so on in my example.
you should create the dictionary from a to z
Expand|Select|Wrap|Line Numbers
  1. alpha2num = { 'a':1 , 'b':2 , 'c': 3 ....'z';:26}
  2.  
if not, show what's the error you got
Mar 2 '07 #9
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     alpha2num = ('a':1 , 'b':2 , 'c':3 , 'd':4 , 'e':5 , 'f':6 , 'g':7 , 'h':8 , 'i':9 , 'j':10 , 'k':11, 'l':12, 'm':13, 'n':14, 'o':15, 'p':16, 'q':17 , 'r':18 , 's':19 , 't':20 , 'u':21 , 'v':22, 'w':23 , 'x':24 , 'y':25 , 'z':26)
  3.     print "Numeric value for a single name"
  4.     name = raw_input("Please enter your first name: ").lower()
  5.     total = 0
  6.     for ch in name:
  7.         total = total + alpha2num[ch]
  8.  
  9.     print total
  10.  
  11. main ()
Keeps saying that the ":" is wrong for some unknown reason. :(
Mar 2 '07 #10
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     alpha2num = ('a':1 , 'b':2 , 'c':3 , 'd':4 , 'e':5 , 'f':6 , 'g':7 , 'h':8 , 'i':9 , 'j':10 , 'k':11, 'l':12, 'm':13, 'n':14, 'o':15, 'p':16, 'q':17 , 'r':18 , 's':19 , 't':20 , 'u':21 , 'v':22, 'w':23 , 'x':24 , 'y':25 , 'z':26)
  3.     print "Numeric value for a single name"
  4.     name = raw_input("Please enter your first name: ").lower()
  5.     total = 0
  6.     for ch in name:
  7.         total = total + alpha2num[ch]
  8.  
  9.     print total
  10.  
  11. main ()
Keeps saying that the ":" is wrong for some unknown reason. :(
A couple of things:
I've added code tags to your post. Please read the "POSTING GUIDELINES" to learn how to do this. It will help us help you.

alpha2num is a dictionary. Dictionaries use curly braces {} not parens (). That should do it.

Also, you can copy for the posts here and paste then in you Py editor (when there are code tags used OR when you hit Reply.
Hope that helps. Keep posting,
Barton
Mar 2 '07 #11
bartonc
6,596 Expert 4TB
Maybe this will help:
Expand|Select|Wrap|Line Numbers
  1. >>> def char_value(character):
  2. ...     return ord(character)-96
  3. ... 
  4. >>> sum = 0
  5. >>> name = 'Bill'
  6. >>> for letter in name:
  7. ...     sum += char_value(letter)
  8. ...     
  9. >>> sum
  10. 3
  11. >>> 
Combine and simplifying two good suggestions:
Expand|Select|Wrap|Line Numbers
  1. def char_value(character):
  2.     return ord(character)-96print "Numeric value for a single name"
  3.  
  4. name = raw_input("Please enter your first name: ")
  5. name = name.lower()
  6. sum = 0
  7. for letter in name:
  8.     sum += char_value(letter)
  9. print sum
  10.  
Mar 2 '07 #12
Yay, I'm so happy, it finally worked thanks to all you guys. :) I was starting to think Python hated me.
Mar 2 '07 #13
bartonc
6,596 Expert 4TB
Yay, I'm so happy, it finally worked thanks to all you guys. :) I was starting to think Python hated me.
Don't keep us in suspence! Post some working code (remember to figure out how to use code tags). You can edit your post for up to 5 minutes to try and get it right. Thanks for joining. I'm glad that you are making progress.
Mar 2 '07 #14
Thanks to everyone's advice, here's the final coding. :)

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     alpha2num = {'a':1 , 'b':2 , 'c':3 , 'd':4 , 'e':5 , 'f':6 , 'g':7 , 'h':8 , 'i':9 , 'j':10 , 'k':11, 'l':12, 'm':13, 'n':14, 'o':15, 'p':16, 'q':17 , 'r':18 , 's':19 , 't':20 , 'u':21 , 'v':22, 'w':23 , 'x':24 , 'y':25 , 'z':26 }
  3.     print "Numeric value for a single name"
  4.     name = raw_input("Please enter your first name: ").lower()
  5.     total = 0
  6.     for ch in name:
  7.         total = total + alpha2num[ch]
  8.     print total
  9. main ()
  10.  
  11.  
Outcome:
Numeric value for a single name
Please enter your first name: Shycat
Numeric value of name:
76
Mar 2 '07 #15
bvdet
2,851 Expert Mod 2GB
Thanks to everyone's advice, here's the final coding. :)

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     alpha2num = {'a':1 , 'b':2 , 'c':3 , 'd':4 , 'e':5 , 'f':6 , 'g':7 , 'h':8 , 'i':9 , 'j':10 , 'k':11, 'l':12, 'm':13, 'n':14, 'o':15, 'p':16, 'q':17 , 'r':18 , 's':19 , 't':20 , 'u':21 , 'v':22, 'w':23 , 'x':24 , 'y':25 , 'z':26 }
  3.     print "Numeric value for a single name"
  4.     name = raw_input("Please enter your first name: ").lower()
  5.     total = 0
  6.     for ch in name:
  7.         total = total + alpha2num[ch]
  8.     print total
  9. main ()
  10.  
  11.  
Outcome:
Numeric value for a single name
Please enter your first name: Shycat
Numeric value of name:
76
Shycat,

I hope you don't mind. Here is a slightly different version:
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     print "Numeric value for a single name"
  3.     name = raw_input("Please enter your first name: ").lower()
  4.     print name
  5.     total = 0
  6.     for ch in name:
  7.         total += ord(ch)-96
  8.     print total
  9. main ()
  10.  
  11. """
  12. >>> Numeric value for a single name
  13. shycat
  14. 76
  15. >>>
  16. """
Happy coding! :)
Mar 2 '07 #16
I'm working on an expanded version of this code and am wondering how you would modify it to accept an entire name or several names rather than just one?

When you enter a single, one word name this program works great. But when you enter any spaces, instead of Python ignoring them, it assigns them a numeric value of -64 which messes up the correct numeric value of the name.

I know the unicode value for a space is 32, but i'm not sure how to tell Python to assign a value of zero to any spaces encountered in a name (or to simply ignore spaces and not assign anything to them).

Heres the code so far. I made it display the value of each letter so I could see if it was adding up the numbers correctly... this also helps to show whats going on with the spaces):

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.  
  3.     print "This program calculates a numeric value for a name.\n"
  4.     name = raw_input("Please enter your first or last name (no spaces): ").lower()
  5.     print "\n","-"*10
  6.     total = 0
  7.     for ch in name:
  8.         total += ord(ch)-96
  9.         print ch,"=",ord(ch)-96
  10.  
  11.     print "-"*10,"\n",name,"=",total
  12.  
  13. main ()
and the result using a full name with spaces:

Expand|Select|Wrap|Line Numbers
  1. This program calculates a numeric value for a name.
  2.  
  3. Please enter your first or last name (no spaces): Joe Schmoe
  4.  
  5. ----------
  6. j = 10
  7. o = 15
  8. e = 5
  9.   = -64
  10. s = 19
  11. c = 3
  12. h = 8
  13. m = 13
  14. o = 15
  15. e = 5
  16. ---------- 
  17. joe schmoe = 29
  18.  
Any help would be most appreciated. Thanks in advance
Feb 10 '08 #17
bvdet
2,851 Expert Mod 2GB
I'm working on an expanded version of this code and am wondering how you would modify it to accept an entire name or several names rather than just one?

When you enter a single, one word name this program works great. But when you enter any spaces, instead of Python ignoring them, it assigns them a numeric value of -64 which messes up the correct numeric value of the name.

I know the unicode value for a space is 32, but i'm not sure how to tell Python to assign a value of zero to any spaces encountered in a name (or to simply ignore spaces and not assign anything to them).

Heres the code so far. I made it display the value of each letter so I could see if it was adding up the numbers correctly... this also helps to show whats going on with the spaces):

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.  
  3.     print "This program calculates a numeric value for a name.\n"
  4.     name = raw_input("Please enter your first or last name (no spaces): ").lower()
  5.     print "\n","-"*10
  6.     total = 0
  7.     for ch in name:
  8.         total += ord(ch)-96
  9.         print ch,"=",ord(ch)-96
  10.  
  11.     print "-"*10,"\n",name,"=",total
  12.  
  13. main ()
and the result using a full name with spaces:

Expand|Select|Wrap|Line Numbers
  1. This program calculates a numeric value for a name.
  2.  
  3. Please enter your first or last name (no spaces): Joe Schmoe
  4.  
  5. ----------
  6. j = 10
  7. o = 15
  8. e = 5
  9.   = -64
  10. s = 19
  11. c = 3
  12. h = 8
  13. m = 13
  14. o = 15
  15. e = 5
  16. ---------- 
  17. joe schmoe = 29
  18.  
Any help would be most appreciated. Thanks in advance
The following code will skip any non-alpha character:
Expand|Select|Wrap|Line Numbers
  1. import string
  2.  
  3. def main():
  4.  
  5.     print "This program calculates a numeric value for a name.\n"
  6.     name = raw_input("Please enter your name: ").lower()
  7.     print "\n","-"*10
  8.     total = 0
  9.     for ch in name:
  10.         if ch in string.ascii_lowercase:
  11.             total += ord(ch)-96
  12.             print ch,"=",ord(ch)-96
  13.  
  14.     print "-"*10,"\n",name,"=",total
  15.  
  16. main ()
Feb 10 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alex | last post by:
Acc 97 Hi, I have the following in my query which works well if it is all set to numbers on table design. But what I want to do is where it states MC (short for machine name) use the actual...
7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
11
by: Pieter | last post by:
Hi, I'm having some troubles with my numeric-types in my VB.NET 2005 application, together with a SQL Server 2000. - I first used Single in my application, and Decimal in my database. But a...
1
by: David | last post by:
I have rows of 8 numerical values in a text file that I have to parse. Each value must occupy 10 spaces and can be either a decimal or an integer. // these are valid - each fit in a 10 character...
1
by: pchadha20 | last post by:
How to single numeric values from a field in a table which has multiple numeric value in a field of a table.And that table contains thousands of records. Suppose we have a table customer having...
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
14
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also...
8
by: Frank Swarbrick | last post by:
My DBA says that a column defined, for instance, as DECIMAL(11,2) and containing a value of 1.00 takes up no more space on the database disk than a column defined as DECIMAL(7,2) and containing a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.