Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortening code with multiple similar "while"-statements

Tags:

python

Very basic question probably, but writing my first program and did not know what to search for to find the answer.

I have a while statement that looks something like this:

while number > 9999 or number < 0 or number == 1111 or number == 2222 or number == 3333...

And goes on until I get to 9999. Lots of code that probably can be shortened, am I correct? Not sure about where I could read about the grammar for this, so someone could also link me there!

Would be glad if anyone could help! :)

like image 296
David Hasselberg Avatar asked Dec 14 '22 08:12

David Hasselberg


1 Answers

Use the modulo operator:

while number > 9999 or number < 0 or (number % 1111 == 0 and number != 0):
like image 82
Mohammed Aouf Zouag Avatar answered Dec 16 '22 23:12

Mohammed Aouf Zouag