mercredi 10 novembre 2021

How to make a function that fills seatings based on surrounding taken/empty seats?

I have the following nested list, which simulates cinema seating:

Zaal:

[
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '\n'], 
['.', 'L', '.', 'L', 'L', '.', 'L', 'L', '.', 'L', 'L', '.', '\n'], 
['.', 'L', 'L', 'L', 'L', 'L', 'L', 'L', '.', 'L', 'L', '.', '\n'], 
['.', 'L', '.', 'L', '.', 'L', '.', '.', 'L', '.', '.', '.', '\n'], 
['.', 'L', 'L', 'L', 'L', '.', 'L', 'L', '.', 'L', 'L', '.', '\n'], 
['.', 'L', '.', 'L', 'L', '.', 'L', 'L', '.', 'L', 'L', '.', '\n'], 
['.', 'L', '.', 'L', 'L', 'L', 'L', 'L', '.', 'L', 'L', '.', '\n'], 
['.', '.', '.', 'L', '.', 'L', '.', '.', '.', '.', '.', '.', '\n'], 
['.', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', 'L', '.', '\n'], 
['.', 'L', '.', 'L', 'L', 'L', 'L', 'L', 'L', '.', 'L', '.', '\n'], 
['.', 'L', '.', 'L', 'L', 'L', 'L', 'L', '.', 'L', 'L', '.', '\n'], 
['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '\n']]

In this list '.' Stands for no seat, 'L' stands for empty seat, when the function runs '#' should stand for a taken seat.

I have the following function to check the number of surrounding seats in which rij stands for column number and stoel stands for row number:

def burenteller(zaal,rij,stoel):
    stoelenbezet=0
    for v in range(rij-1, rij+2):
        for h in range(stoel-1, stoel+2):
            if v>=0 and v<=aantalrijen and h>=0 and h<stoelenperrij:
                if v!=rij or h!=stoel:
                    if zaal[v][h]=='L':
                        stoelenbezet+=1
    return(stoelenbezet) 

The following code I want to run multiple times to simulate persons moving positions in a crowded seat, which has three erules: A seat will get taken if all surrounding seats are empty, and a seat will move from '#' (taken) to 'L' empty if the surrounding seats contain four # I do this by making and empty list (Zaal2)

#krijgen juiste rij

for elkerij in range(aantalrijen):
#krijgen juiste stoel
    for elkestoel in range(stoelenperrij):
#checken voor voorwaarde
        if elkestoel == '.':
            Zaal2.append('.')
    # Als element L is en burenteller geeft 0 -> element wordt #
        elif burenteller(Zaal,elkerij,elkestoel)== 0 and elkestoel == 'L':
            #if element == 'L':
                Zaal2.append('#') 
    # Als element # is en burenteller geeft 4 of meer aan -> element wordt #
        elif burenteller(Zaal,elkerij,elkestoel)>= 4 and elkestoel == '#':
            #if element == '#':
                Zaal2.append('L')

I get the following error:

IndexError                                Traceback (most recent call last)
<ipython-input-8-62e4580e39c2> in <module>
      8             Zaal2.append('.')
      9     # Als element L is en burenteller geeft 0 -> element wordt #
---> 10         elif burenteller(Zaal,elkerij,elkestoel)== 0 and elkestoel == 'L':
     11             #if element == 'L':
     12                 Zaal2.append('#')

<ipython-input-4-c270a8cd4d30> in burenteller(zaal, rij, stoel)
     13             if v>=0 and v<=aantalrijen and h>=0 and h<stoelenperrij:
     14                 if v!=rij or h!=stoel:
---> 15                     if zaal[v][h]=='L':
     16                         stoelenbezet+=1
     17     return(stoelenbezet)

IndexError: list index out of range

Aucun commentaire:

Enregistrer un commentaire