zhengheng lin
zhengheng lin

Reputation: 35

Python calculate many different sum in for loop

I was trying to calculate letter value (a = 1, b = 2...z = 26) of input. any other characters are considered 0 the code is listed below:

alpha = list('abcdefjhijklmnopqrstuvwxyz')
a = []
sum1= 0
while True:
    b = input()
    if b == '':
        break
    else:
        a.append(b.lower())
lst_len = len(a)
for i in range(lst_len):
    elem_len = len(a[i])
    for j in range(elem_len):
        if (a[i][j]) in alpha:
            num = alpha.index(a[i][j]) + 1
            sum1 += num

        else:
            pass
    print sum1

my question is how do I print the sum value of each i in for loop instead of keep adding to the previous sums

Thank you!

Upvotes: 2

Views: 103

Answers (3)

Tezirg
Tezirg

Reputation: 1639

Use a temporary sum var :

alpha = list('abcdefjhijklmnopqrstuvwxyz')
a = []
sum1= 0
while True:
    b = input()
    if b == '':
        break
    else:
        a.append(b.lower())
lst_len = len(a)
for i in range(lst_len):
    local_sum = 0
    elem_len = len(a[i])
    for j in range(elem_len):
        if (a[i][j]) in alpha:
            num = alpha.index(a[i][j]) + 1
            local_sum += num
        else:
            pass
    print local_sum
    sum1 += local_sum
print sum1

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

Use a dictionary, mapping each letter to the correct value, then use dict.get on each character in each word using 0 as the default values for missing keys and summing all the values for each word:

alpha = dict(enumerate('abcdefjhijklmnopqrstuvwxyz',1))
a = []

while True:
    b = input("Press 'enter' when finished")
    if b == '':
        break
    else:
        a.append(b.lower())
for word in a:
    print(sum(alpha.get(ch,0) for ch in word))

You can also use iter and a lit comp to take the input:

alpha = dict(enumerate('abcdefjhijklmnopqrstuvwxyz',1))
a = [w for w in iter(lambda:input("Press 'enter' when finished").lower(),"")]

The second arg to iter is a sentinel value that will break the loop when entered.

for word in a:
    # sum all letter values, chars not in out dict will get 0
    print(sum(alpha.get(ch,0) for ch in word))

alpha will have each letter as a key and values from 1-26:

{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'j', 8: 'h', 9: 
'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 
17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}

On a side note you don't need to call list on a string, strings are iterable and indexable so alpha = 'abcdefjhijklmnopqrstuvwxyz' would have sufficed.

You also don't need an else after every if unless you actually want to do something else so you can remove the else:pass.

Lastly instead of using if (a[i][j]) in alpha: and then indexing you could have used str.find and just checked the return value was not == to -1 which is the default when there is no match.

Upvotes: 2

Aereaux
Aereaux

Reputation: 855

Move sum1 = 0 to after the for i in range(lst_len):, so it gets reset after every time the inner loop finishes.

Upvotes: 1

Related Questions