[英]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.谁能告诉我为什么我在 Python 2.7 中不断收到此错误,请参阅下面的错误标记。 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:您已将 DC 声明为整数,但在此行中:
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.我认为你想要的是摆脱你在 while 循环中所做的所有索引分配。
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. DC2
是一个整数。 You're trying to assign to it as if it were a list
(with indexing).您试图将它分配给它,就好像它是一个list
(带有索引)。 That doesn't work.那行不通。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.