mercredi 21 décembre 2016

Improve loop with if statements

I have a problem try to make my code a little bit more efficient.

for i in range(11):
    for j in range(11):
        if i==0 or i==11 or j==0 or j==11:
            do something

        elif i==1 or i==9:
            if j==1 or j==2 or j==3 or j==7 or j==8 or j==9:
                do A
            else:
                do B
        elif i==2 or i==8:
            if j==1 or j==2 or j==8 or j==9:
                do A
            else:
                do B
        elif i==3 or i==7:
            if j==1 or j==9:
                do A
            else:
                do B
        else:
            do B

As you can see, I have managed to code it by hand, but I have to make the range bigger, so there will be a lot (seriously, I have to do about 1000 elif's) so there is no way I'm gonna code it by hand. Is there some way, like doing another loop or defining some sort of function, to simplify it??

Thanks

Aucun commentaire:

Enregistrer un commentaire