Python: Allow for empty if else blocks
By Eric Downing. Filed in Programming, Python, Scripting |I was toying with a prime number finder and was creating a set of if/else statements for a horribly failed attempt
and wanted to skip to the next number on any of the if statements that were true. I didn’t want to put an empty print
statement as that would disrupt the output I was expecting from the program. I found the pass statement.
Here is an example from my not very successful attempt at finding primes:
if inp > 2 and inp % 2 == 0: pass elif inp > 3 and inp % 3 == 0: pass elif inp > 7 and inp % 7 == 0: pass elif inp > 9 and inp % 9 == 0: pass else: print inp, " is a prime"
Yes, this does not accurately find the primes, but it would skip any number that is divisible by 2,3,5,7, or 9. And
anything that does not match this will be listed as a prime, this is incorrect and would be made to add new elif
statements as each prime was found.