[newbie/2.5.1.1] Computing value of a word?

Discussion in 'Python' started by Gilles Ganault, Feb 16, 2011.

  1. Hello,

    For a game, I need to go through a wordlist, and for each word,
    compute its value, ie. a=1, b=2, etc.

    So for instance, NewYork = 14 + 5 + 23 + 25 + 15 + 18 + 11 = 111.

    Before I write the obvious While loop to go through each line in the
    input text file, I was wondering if Python didn't already have some
    function to perform this type of computation.

    Thank you.
     
    Gilles Ganault, Feb 16, 2011
    #1
    1. Advertising

  2. Gilles Ganault

    Chris Rebert Guest

    On Wed, Feb 16, 2011 at 1:17 AM, Gilles Ganault <> wrote:
    > Hello,
    >
    > For a game, I need to go through a wordlist, and for each word,
    > compute its value, ie. a=1, b=2, etc.
    >
    > So for instance, NewYork = 14 + 5 + 23 + 25 + 15 + 18 + 11 = 111.
    >
    > Before I write the obvious While loop to go through each line in the
    > input text file, I was wondering if Python didn't already have some
    > function to perform this type of computation.


    A = ord('a') - 1
    for line in your_file:
    word = line.strip().lower()
    score = sum(ord(letter)-A for letter in word)

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Feb 16, 2011
    #2
    1. Advertising

  3. On Wed, 16 Feb 2011 01:27:06 -0800, Chris Rebert <>
    wrote:
    >A = ord('a') - 1
    >for line in your_file:
    > word = line.strip().lower()
    > score = sum(ord(letter)-A for letter in word)


    Thanks much Chris.
     
    Gilles Ganault, Feb 17, 2011
    #3
    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. Torsten Landschoff
    Replies:
    8
    Views:
    2,821
    Torsten Landschoff
    Feb 22, 2007
  2. Raider

    computing average value

    Raider, Feb 26, 2006, in forum: C++
    Replies:
    5
    Views:
    365
    Raider
    Feb 27, 2006
  3. optical supercomputing

    Optical Computing: special issue - Natural Computing, Springer

    optical supercomputing, Dec 19, 2008, in forum: C Programming
    Replies:
    0
    Views:
    424
    optical supercomputing
    Dec 19, 2008
  4. optical supercomputing

    Optical Computing: special issue - Natural Computing, Springer

    optical supercomputing, Jan 16, 2009, in forum: C Programming
    Replies:
    0
    Views:
    451
    optical supercomputing
    Jan 16, 2009
  5. Alex Shulgin
    Replies:
    15
    Views:
    235
    Robert Klemme
    Sep 18, 2007
Loading...

Share This Page