numeric value of a single name.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shycat518
    New Member
    • Mar 2007
    • 11

    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. :(
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by shycat518
    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:
    Code:
    >>> 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.

    Comment

    • shycat518
      New Member
      • Mar 2007
      • 11

      #3
      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("Plea se 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.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by shycat518
        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("Plea se 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:
        Code:
        >>> def char_value(character):
        ... 	return ord(character)-96
        ... 
        >>> sum = 0
        >>> name = 'Bill'
        >>> for letter in name:
        ... 	sum += char_value(letter)
        ... 	
        >>> sum
        3
        >>>

        Comment

        • shycat518
          New Member
          • Mar 2007
          • 11

          #5
          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.

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            Originally posted by shycat518
            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
            Code:
            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

            Comment

            • dshimer
              Recognized Expert New Member
              • Dec 2006
              • 136

              #7
              If you use the ord() examples, you can force the case before the letters are evaluated using either upper() or lower().

              Code:
              >>> Astring='SomeLetters'
              >>> Astring.upper()
              'SOMELETTERS'
              >>> Astring.lower()
              'someletters'

              Comment

              • shycat518
                New Member
                • Mar 2007
                • 11

                #8
                Originally posted by ghostdog74
                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
                Code:
                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.

                Comment

                • ghostdog74
                  Recognized Expert Contributor
                  • Apr 2006
                  • 511

                  #9
                  Originally posted by shycat518
                  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
                  Code:
                  alpha2num = { 'a':1 , 'b':2 , 'c': 3 ....'z';:26}
                  if not, show what's the error you got

                  Comment

                  • shycat518
                    New Member
                    • Mar 2007
                    • 11

                    #10
                    Code:
                    def main():
                        alpha2num = ('a'[B]:[/B]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. :(
                    Last edited by bartonc; Mar 2 '07, 12:45 AM. Reason: added [code][/code] tags

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by shycat518
                      Code:
                      def main():
                          alpha2num = ('a'[B]:[/B]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

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by bvdet
                        Maybe this will help:
                        Code:
                        >>> 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:
                        Code:
                        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

                        Comment

                        • shycat518
                          New Member
                          • Mar 2007
                          • 11

                          #13
                          Yay, I'm so happy, it finally worked thanks to all you guys. :) I was starting to think Python hated me.

                          Comment

                          • bartonc
                            Recognized Expert Expert
                            • Sep 2006
                            • 6478

                            #14
                            Originally posted by shycat518
                            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.

                            Comment

                            • shycat518
                              New Member
                              • Mar 2007
                              • 11

                              #15
                              Thanks to everyone's advice, here's the final coding. :)

                              Code:
                              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

                              Comment

                              Working...