【发布时间】:2015-10-30 21:49:41
【问题描述】:
我有一个数组,其中数组中的每个位置都代表一个停车位。数组(或停车场)中每个位置的值表示停车位(汽车、摩托车等)中有什么。 showSpots 函数应显示特定车辆所在的所有位置。例如,showSpots(CAR)(其中 CAR = 1)应显示所有值为 1 的位置(或数组中的位置)。
但是,当我运行代码时,我在“if holder in parkLot[x]:”行上得到一个“TypeError:'int'类型的参数不可迭代'”
为什么会出现此错误,我该如何解决?
我的代码:
from random import *
parkingLot = [0, 1, 0, 2, 0, 0, 1]
EMPTY = 0
CAR = 1
MOTORCYCLE = 2
def showSpots(holder):
for x in range(0, 6):
if holder in parkingLot[x]:
print(x)
def main():
print("There are cars in lots:")
showSpots(CAR)
print("There are motorcycles in lots:")
showSpots(MOTORCYCLE)
main()
【问题讨论】: