【发布时间】:2011-10-11 15:27:41
【问题描述】:
为什么下面的脚本会报错:
payIntList[i] = payIntList[i] + 1000
TypeError: 'map' object is not subscriptable
payList = []
numElements = 0
while True:
payValue = raw_input("Enter the pay amount: ")
numElements = numElements + 1
payList.append(payValue)
choice = raw_input("Do you wish to continue(y/n)?")
if choice == 'n' or choice == 'N':
break
payIntList = map(int,payList)
for i in range(numElements):
payIntList[i] = payIntList[i] + 1000
print payIntList[i]
【问题讨论】:
-
你在使用 Python 3 吗?
-
@user567797 - 这对我来说很好@Felix:他没有使用 python 3,因为他使用 print 作为语句!
-
while 循环下方的整个内容可以缩短为
payIntList = [int(x) + 1000 for x in payList]; print(*payIntList, sep='\n')(或在 Python 2.x 中为for x in payIntList: print x,其中print不是函数)而不会失去可读性(可以说,它甚至是更具可读性)。 -
@Guanidene:他使用的是 Python 3,因为他有地图对象。但他正试图在其上运行 Python 2 代码,因此出现了错误。
-
我想要一个“为什么我在 Python 3 上运行 Python 2 代码时会出错”的问题,我们可以将所有这些标记为重复项。 ;)
标签: python python-3.x