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. :(
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: - >>> def char_value(character):
-
... return ord(character)-96
-
...
-
>>> char_value('A')
-
-31
-
>>> char_value('a')
-
1
-
>>> char_value('b')
-
2
-
>>>
Perhaps you could post some of your own code to give us a better understanding of what you are trying to accomplish.
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.
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: - >>> def char_value(character):
-
... return ord(character)-96
-
...
-
>>> sum = 0
-
>>> name = 'Bill'
-
>>> for letter in name:
-
... sum += char_value(letter)
-
...
-
>>> sum
-
3
-
>>>
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.
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 -
-
alpha2num = { 'a' : 1, 'b' : 2, #and so on}
-
print "Numeric value for a single name"
-
name = raw_input("Please enter your first name: ").lower()
-
total = 0
-
for ch in name:
-
total = total + alpha2num[ch]
-
-
print total
-
-
If you use the ord() examples, you can force the case before the letters are evaluated using either upper() or lower(). - >>> Astring='SomeLetters'
-
>>> Astring.upper()
-
'SOMELETTERS'
-
>>> Astring.lower()
-
'someletters'
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 -
-
alpha2num = { 'a' : 1, 'b' : 2, #and so on}
-
print "Numeric value for a single name"
-
name = raw_input("Please enter your first name: ").lower()
-
total = 0
-
for ch in name:
-
total = total + alpha2num[ch]
-
-
print total
-
-
I've tried the 'a' : 1 before, but for some odd reason python keeps saying it's wrong.
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 -
alpha2num = { 'a':1 , 'b':2 , 'c': 3 ....'z';:26}
-
if not, show what's the error you got
- def main():
-
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)
-
print "Numeric value for a single name"
-
name = raw_input("Please enter your first name: ").lower()
-
total = 0
-
for ch in name:
-
total = total + alpha2num[ch]
-
-
print total
-
-
main ()
Keeps saying that the ":" is wrong for some unknown reason. :(
- def main():
-
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)
-
print "Numeric value for a single name"
-
name = raw_input("Please enter your first name: ").lower()
-
total = 0
-
for ch in name:
-
total = total + alpha2num[ch]
-
-
print total
-
-
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
Maybe this will help: - >>> def char_value(character):
-
... return ord(character)-96
-
...
-
>>> sum = 0
-
>>> name = 'Bill'
-
>>> for letter in name:
-
... sum += char_value(letter)
-
...
-
>>> sum
-
3
-
>>>
Combine and simplifying two good suggestions: - def char_value(character):
-
return ord(character)-96print "Numeric value for a single name"
-
-
name = raw_input("Please enter your first name: ")
-
name = name.lower()
-
sum = 0
-
for letter in name:
-
sum += char_value(letter)
-
print sum
-
Yay, I'm so happy, it finally worked thanks to all you guys. :) I was starting to think Python hated me.
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.
Thanks to everyone's advice, here's the final coding. :) -
def main():
-
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 }
-
print "Numeric value for a single name"
-
name = raw_input("Please enter your first name: ").lower()
-
total = 0
-
for ch in name:
-
total = total + alpha2num[ch]
-
print total
-
main ()
-
-
Outcome:
Numeric value for a single name
Please enter your first name: Shycat
Numeric value of name:
76
bvdet 2,851
Expert Mod 2GB
Thanks to everyone's advice, here's the final coding. :) -
def main():
-
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 }
-
print "Numeric value for a single name"
-
name = raw_input("Please enter your first name: ").lower()
-
total = 0
-
for ch in name:
-
total = total + alpha2num[ch]
-
print total
-
main ()
-
-
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: - def main():
-
print "Numeric value for a single name"
-
name = raw_input("Please enter your first name: ").lower()
-
print name
-
total = 0
-
for ch in name:
-
total += ord(ch)-96
-
print total
-
main ()
-
-
"""
-
>>> Numeric value for a single name
-
shycat
-
76
-
>>>
-
"""
Happy coding! :)
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): - def main():
-
-
print "This program calculates a numeric value for a name.\n"
-
name = raw_input("Please enter your first or last name (no spaces): ").lower()
-
print "\n","-"*10
-
total = 0
-
for ch in name:
-
total += ord(ch)-96
-
print ch,"=",ord(ch)-96
-
-
print "-"*10,"\n",name,"=",total
-
-
main ()
and the result using a full name with spaces: -
This program calculates a numeric value for a name.
-
-
Please enter your first or last name (no spaces): Joe Schmoe
-
-
----------
-
j = 10
-
o = 15
-
e = 5
-
= -64
-
s = 19
-
c = 3
-
h = 8
-
m = 13
-
o = 15
-
e = 5
-
----------
-
joe schmoe = 29
-
Any help would be most appreciated. Thanks in advance
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): - def main():
-
-
print "This program calculates a numeric value for a name.\n"
-
name = raw_input("Please enter your first or last name (no spaces): ").lower()
-
print "\n","-"*10
-
total = 0
-
for ch in name:
-
total += ord(ch)-96
-
print ch,"=",ord(ch)-96
-
-
print "-"*10,"\n",name,"=",total
-
-
main ()
and the result using a full name with spaces: -
This program calculates a numeric value for a name.
-
-
Please enter your first or last name (no spaces): Joe Schmoe
-
-
----------
-
j = 10
-
o = 15
-
e = 5
-
= -64
-
s = 19
-
c = 3
-
h = 8
-
m = 13
-
o = 15
-
e = 5
-
----------
-
joe schmoe = 29
-
Any help would be most appreciated. Thanks in advance
The following code will skip any non-alpha character: - import string
-
-
def main():
-
-
print "This program calculates a numeric value for a name.\n"
-
name = raw_input("Please enter your name: ").lower()
-
print "\n","-"*10
-
total = 0
-
for ch in name:
-
if ch in string.ascii_lowercase:
-
total += ord(ch)-96
-
print ch,"=",ord(ch)-96
-
-
print "-"*10,"\n",name,"=",total
-
-
main ()
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |