5

Is there a better solution to write a while loop that runs forever if the argument is 0 or just runs n times if the argument is an arbitrary n larger 0 than this:

x = options.num  # this is the argument (read by Optparse)
if x == 0:
    check = lambda x: True
else:
    check = lambda x: True if x > 0 else False
while check(x):
    print("Hello World")
    x -= 1

you can probably combine the lambda into:

check = lambda x: True if x > 0 or options.num == 0 else False

but then you will still have to count down x, unless you put a if before that.

reox
  • 5,036
  • 11
  • 53
  • 98

5 Answers5

6

How about:

n = options.num

while (options.num == 0) or (n > 0):
    print("Hello World")
    n -= 1

Essentially we have an infinite loop if x == 0 or we only run n times.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
James Mills
  • 18,669
  • 3
  • 49
  • 62
4

I think this is quite expressive:

if x==0:
  x = float("inf") # user says 0 but means infinity

while x>0:
   print "something"
   x -= 1
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64
  • 2
    +1 Yes, that's exactly what I was just writing down, too. ;-) But how about changing that to `x = options.num or float('inf')`? Also see [here](http://stackoverflow.com/questions/1628026/python-infinity-any-caveats) – tobias_k Jul 18 '14 at 09:05
  • @tobias_k I think I prefer like it is now. Using a valid value n=0 as a placeholder is, in my opinion, a wrong choice which must me clearly stated in the code. – Emanuele Paolini Jul 18 '14 at 14:12
2

Try this

def while_function(n):
    if n > 0: n += 1
    while n-1:
        print "something"
        n -= 1

Here is demo:

>>> while_function(1)
something
>>> while_function(2)
something
something
>>> while_function(0)
something
something
.
.
.
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
2

Use itertools.count

The trick is easy:

from itertools import count
x = options.num  # this is the argument (read by Optparse)
if x:
    loops = xrange(x)
else:
    loops = count()

for i in loops:
    print("Hello World")

The code runs looping over iterator. In case of number other than 0, it uses iterator xrange, which yields x numbers.

In case of x being 0, we ouse count iterator from itertools, which is able to retreive infinite number of numbers.

It can be even shortened (but I leave original version up for readibility):

from itertools import count
x = options.num  # this is the argument (read by Optparse)

for i in xrange(x) if x else count():
    print("Hello World")
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • +1 Nice. Maybe shorter with `loops = xrange(options.num) if options.num else count()` – tobias_k Jul 18 '14 at 09:50
  • @tobias_k Yes, I had similar construct in my editor, but decided to go in more readable way. Short variant added as you have proposed (with leaving `x = options.num` on separate line) – Jan Vlcinsky Jul 18 '14 at 10:19
0

Consider focusing on readability instead of minimal length of the code. If a body of the loop is trivial I would write

if x:
  for i in xrange(x):
    print('Hello, World!')
else:
  while True:
    print('Hello, World!')

And if the body is big enough I would extract a function

def body():
  print('Hello, World!')

if x:
  for i in xrange(x):
    body()
else:
  while True:
    body()
Kirill
  • 3,364
  • 2
  • 21
  • 36