New to Python. Background from TRS basic thru several versions of basic and ending with
Qbasic, some Perl and now trying Python.
I get the following error and have no idea what is causing it.
There would be 11 catagories and some catagories could have multiple entries for the period.
tia
otto

<

Traceback (most recent call last):
  File "Forum-Code.py", line 87, in <module>
    tcol1[mm] = col1 + amt
TypeError: 'int' object does not support item assignment

/> <

F-expenses-18
12418,1,119.61,2

/> <

#!/usr/bin/python
#
col1 = 0
col2 = 0
col3 = 0
col4 = 0
col5 = 0
col6 = 0
col7 = 0
col8 = 0
col9 = 0
col10 = 0
col11 = 0
#
mtotals = {}
tcol1 = {}
tcol2 = {}
tcol3 = {}
tcol4 = {}
tcol5 = {}
tcol6 = {}
tcol7 = {}
tcol8 = {}
tcol9 = {}
tcol10 = {}
tcol11 = {}
#
tcol1 =  0
tcol2 =  0
tcol3 =  0
tcol4 =  0
tcol5 =  0
tcol6 =  0
tcol7 =  0
tcol8 =  0
tcol9 =  0
tcol10 =  0
tcol11 =  0
#
stcol1 = 0
stcol2 = 0
stcol3 = 0
stcol4 = 0
stcol5 = 0
stcol6 = 0
stcol7 = 0
stcol8 = 0
stcol9 = 0
stcol10 = 0
stcol11 = 0
#
x = 0
amt = 0
tamt = 0
howp1 = 0
howp2 = 0
howp3 = 0
mm = 1
startmm = 1
#
with open('F-expenses-18', 'r') as file:
for line in file:
    (pdate, catg, amt, howp) = line.split(',')
    pdate = str(pdate)
    pdate = pdate.strip()
    length = len(pdate)
    if length == 5:
       mm = pdate[0:1]
       if mm != startmm:
          startmm = mm
    elif length == 6:
      mm = pdate[0:2]
      if mm != startmm:
         startmm = mm
    startmm = mm
    catg = int(catg)
    amt = float(amt)
    tamt = tamt + amt
    howp = int(howp)
    if catg == 1:
      tcol1[mm] = col1 + amt
      stcol1 = stcol1 + amt
    elif catg == 2:
      tcol2[mm] = col2 + amt
      stcol2 = stcol2 + amt
    elif catg == 3:
      tcol3[mm] = col3 + amt
      stcol3 = stcol3 + amt
    elif catg == 4:
      tcol4[mm] = col4 + amt
      stcol4 = stcol4 + amt
      cnt4 = cnt4 + 1
    elif catg == 5:
      tcol5[mm] = col5 + amt
      stcol5 = stcol5 + amt
    elif catg == 6:
      tcol6[mm] = col6 + amt
      stcol6 = stcol6 + amt
    elif catg == 7:
      tcol7[mm] = col7 + amt
      stcol7 = stcol7 + amt
    elif catg == 8:
      tcol8[mm] = col8 + amt
      stcol8 = stcol8 + amt
    elif catg == 9:
      tcol9[mm] = col9 + amt
      stcol9 = stcol9 + amt
    elif catg == 10:
      tcol10[mm] = col10 + amt
      stcol10 = stcol10 + amt
    elif catg == 11:
      col11 = 0
      tcol11[mm] = col11 + amt
      stcol11 = stcol11 + amt
#
    if howp == 1:
      howp1 = howp1 + 1
    elif howp == 2:
      howp2 = howp2 + 1
    elif howp == 3:
      howp3 = howp3 + 1

print 'pdate > ',pdate
print '\n mm > ',mm
print '\n catg > ',catg
print '\n amt > ',amt
print '\n howp > ',howp2
print '\n tcol1mm > ',tcol1[mm]
print '\n stcol1 > ',stcol1
/>

tcol1 = 0
tcol1[mm] = col1 + amt

You can't set a member of 0 to a value. You are essentially saying 0[mm] = col1 + amt;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.