ord function problem from newbie

Discussion in 'Python' started by David.J.Anderson66@gmail.com, Mar 18, 2008.

  1. Guest

    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
     
    , Mar 18, 2008
    #1
    1. Advertising

  2. Steven Clark Guest

    print sum([ord(ch)-96 for ch in small])

    On Mon, Mar 17, 2008 at 11:28 PM, <> wrote:
    > 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
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Steven Clark, Mar 18, 2008
    #2
    1. Advertising

  3. Paul Rubin Guest

    writes:
    > 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
     
    Paul Rubin, Mar 18, 2008
    #3
  4. 7stud Guest

    wrote:
    > 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
     
    7stud, Mar 18, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angus Comber
    Replies:
    7
    Views:
    1,193
    Richard Heathfield
    Feb 5, 2004
  2. Replies:
    20
    Views:
    880
    Nobody
    Aug 30, 2009
  3. Sean McIlroy

    chr / ord

    Sean McIlroy, Nov 3, 2009, in forum: Python
    Replies:
    3
    Views:
    557
    Sean McIlroy
    Nov 3, 2009
  4. Brian Candler

    1.9 inverse of String#ord

    Brian Candler, Jul 30, 2009, in forum: Ruby
    Replies:
    2
    Views:
    90
    Brian Candler
    Jul 30, 2009
  5. Xavier Noria

    what is String#ord?

    Xavier Noria, Apr 17, 2010, in forum: Ruby
    Replies:
    3
    Views:
    143
    Xavier Noria
    Apr 17, 2010
Loading...

Share This Page