Python 3.4 TypeError: argument of type 'int' does not repeat

I have an array where each position in the array represents a parking spot. The value of each position in the array (or car park) is what is parked (car, motorcycle, etc.). The showSpots function should show all the spots on which a particular vehicle is located. For example, showSpots (CAR) (where CAR = 1) should show all spots (or positions in the array) that contain a value of 1.

However, when I run the code, I get a 'TypeError: argument of type' int ', which is not iterable' in the string ', if the owner is in parkingLot [x]:'

Why am I getting this error and how to fix it?

My code is:

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()
+4
4

, if holder == parkingLot[x]:. in, Python parkingLot[x] , holder. , list(1), .

+5

parkingLot[x] - , in (.. , , ..)

+1

if holder == parkingLot[x]. , , - , .

==.

+1

TerryA .

Python ​​ , .

. :

def showSpots(holder):
  for space in [ index for index,value in enumerate(parkingLot) if value == holder ]:
    print (space)

The function enumerateconverts your input into index / value pairs, which you can use to build a list containing only the data that you are interested in. Since you never need to directly manipulate an input list, you need not worry about the size of the list and the inability to mix iterative and indestructible values.

+1
source

Source: https://habr.com/ru/post/1613905/


All Articles