# [newbie/2.5.1.1] Computing value of a word?

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

1. ### Gilles GanaultGuest

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

2. ### Chris RebertGuest

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

3. ### Gilles GanaultGuest

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