[Tutor] Addition, Dictionary, KeysError

Rilindo Foster webmaster at monzell.com
Sat Sep 13 21:53:34 CEST 2008


Another silly question (if you were to call it a question)

Pulling the list of Store Orders and Sales and I am attempting to  
accumulate the total amount of sales per Store.

In Perl, if I were to iterate through a hash, I can easily add the  
values and populate the hash with:

TotalSales{Store} = TotalSales{Store} + Sales

In Python, it won't let me do that. If I were to do what I have done  
in Perl:

OrderDict[o[0]] = float(OrderDict[o[0]]) + float(o[1])

I kept getting a "KeyError".

Am I doing this right? For some reason, I think I know the answer, but  
I can't put my finger on it. 


More information about the Tutor mailing list