Python loop with default value otherwise infinite
Question: I want to create a loop which runs n-times where:

  1. 'n' can be changed freely
  2. 'n' should have a default value when no value is passed
  3. 'n' should run forever when wanted

My current code looks like this:

    
    
    n = raw_input('number of runs: ')       # Get user input for 'n'
    if n == '':                             # Empty input -> run the loop 1000 times (condition 2)
        a = 1000                            # Default value
    elif n == 'oo':                         # Run the loop forever
        a = 9000                            # This value isn't important could be 1 aswell see while statement
    else:
        a = n                               # Run the loop for the value provided in raw_input
    i = 1
    while i <= int(a):
        if n == 'oo': 
            a += 1                          # If n=∞, increment 'a' after each iteration so the loop never stops
        print i                             # Print 'i' to see if everything works like expected
        i += 1
    

How can I make this code more elegant (I feel like there is an easier way)?
How can I make my loop (for n='oo') stop whenever I want without using a
keyboard interrupt?


Answer: Use a `for`-loop, and different iterables:

    
    
    from itertools import count
    
    n = raw_input('number of runs: ')
    if n == '':     #empty input-> run the loop 1000 times
        counter = xrange(1, 1001)
    elif n == 'oo': #run the loop forever
        counter = count(1)
    else:
        counter = xrange(1, int(n)+1)   #otherwise run the loop for the value provided above
    for i in counter:
        print i    #print i to see if everything works like expected
    

