简体   繁体   中英

TypeError: 'int' object does not support item assignment Python

Can anyone please tell me why I am constantly getting this error in Python 2.7, See below for the marking of error. Thanks in advance!

I have to make a program to calculate the amount of coins per person and figure out their totals

TotalPeople = int(raw_input("Enter the number of people "))
Loop = 0
DC2 = 0
DC1 = 0
C50 = 0
C25 = 0 
C10 = 0
C5 = 0 
C2 = 0 
TVC = 0.0
i = 1

while i != TotalPeople:
    DC2[i] = raw_input("How many 2 dollar coins?")     // ERROR for this line
    DC1[i] = raw_input("How many 1 dollar coins?")
    C50[i] = raw_input("How many 50 cent coins?")
    C25[i] = raw_input("How many 25 cent coins?")
    C10[i] = raw_input("How many 10 cent coins?")
    C5[i] = raw_input("How many 5 cent coins?")
    C2[i] = raw_input("How many 2 cent coins?")
    Total =  (DC2[i] *2) + (DC1[i] * 1) + (C50[i] * 0.50) + (C25[i] * 0.25) + (C10[i] * 0.10) + (C5[i] * 0.05) + (C2[i] * 0.02)
    print "The total is " + Total

iw = 1
while iw != TotalPeople:
    TVC = TVC + (DC2[iw] * 2)
    TVC = TVC + (DC1[iw] * 1) 
    TVC = TVC + (C50[iw] * 0.50) 
    TVC = TVC + (C25[iw] * 0.25) 
    TVC = TVC + (C10[iw] * 0.10) 
    TVC = TVC + (C5[iw] *  0.05) 
    TVC = TVC + (C2[iw] * 0.02)

print("The total value is CAD $" + str(TVC) + " for " + str(TotalPeople) +  " people") 

You have declared DC as a integer but then in this line:

DC2[i] = raw_input("How many 2 dollar coins?")

(and all the consecutive lines ahead), you are dealing with it as if it was a suscriptable object. I think that what you want is to get rid of all the index assignment you're doing inside the while loop.

DC2 = raw_input("How many 2 dollar coins?") 
DC1 = raw_input("How many 1 dollar coins?")
C50 = raw_input("How many 50 cent coins?")
C25 = raw_input("How many 25 cent coins?")
C10 = raw_input("How many 10 cent coins?")
C5 = raw_input("How many 5 cent coins?")
C2 = raw_input("How many 2 cent coins?")

DC2 is an integer. You're trying to assign to it as if it were a list (with indexing). That doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM