I am doing a program which reads a file (which contains in lines words, is attached) and has to return a new file with the word in question and the sum of its characters (these I generated from a dictionary ). The problem is that it does everything but accumulates the sums, that is, from the value of the previous word, adds the new value instead of zero. Could someone help me solve this problem?
IMAGE OF THE TEXT FILE:
CODE:
def glich(pal):
global c
valores={}
for i in range (27):
abc = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
valores[abc[i]] = i
palabra = pal.upper()
#c = 0
for letra in palabra:
a = valores[letra]
c = c+a
return c
c=0
arch=open("palabras.txt","r")
val=[]
palas=[]
for linea in arch:
glich(linea.rstrip("\n"))
palas.append(linea.rstrip("\n"))
val.append(c)
arch.close()
nuevo=open("suma.txt","w")
for i in range(len(palas)):
nuevo.write(palas[i]+","+str(val[i])+"\n")
HOW TO LEAVE:
HOW IT COMES OUT: