Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can this very long if-statement be simplified?

How can this if-statement be simplified? It makes a plus sign: http://i.stack.imgur.com/PtHO1.png

If the statement is completed, then a block is set at the x and y coordinates.

for y in range(MAP_HEIGHT):
    for x in range(MAP_WIDTH):
        if (x%5 == 2 or x%5 == 3 or x%5 == 4) and \
            (y%5 == 2 or y%5 == 3 or y%5 == 4) and \
            not(x%5 == 2 and y%5 == 2) and \
            not(x%5 == 4 and y%5 == 2) and \
            not(x%5 == 2 and y%5 == 4) and \
            not(x%5 == 4 and y%5 == 4):
            ...
like image 464
Bryce Guinta Avatar asked Nov 03 '10 14:11

Bryce Guinta


People also ask

How do you simplify long IF statements in Python?

Use a dictionary to simplify a long if statement Since the dictionary maps strings to functions, the operation variable would now contain the function we want to run. All that's left is to run operation(numbers) to get our result. If the user entered 'add' , then operation will be the sum function.

How do you reduce if else in Python?

Use If/Else Statements in One Line To simplify the code and reduce the number of lines in the conditional statements, you can use an inline if conditional statement. Consider the following concise code that performs the same with one line for each if/else conditional statement.


1 Answers

This is the same:

if (x % 5 == 3 and y % 5 > 1) or (y % 5 == 3 and x % 5 > 1): 
like image 113
Dave Webb Avatar answered Oct 27 '22 10:10

Dave Webb