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):
...
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.
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.
This is the same:
if (x % 5 == 3 and y % 5 > 1) or (y % 5 == 3 and x % 5 > 1):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With