Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While loop doesn't stop

Tags:

python

I have this simple code in Python:

import sys

class Crawler(object):

  def __init__(self, num_of_runs):
    self.run_number = 1
    self.num_of_runs = num_of_runs

  def single_run(self):
    #do stuff
    pass

  def run(self):
    while self.run_number <= self.num_of_runs:
      self.single_run()
      print self.run_number
      self.run_number += 1

if __name__ == "__main__":
  num_of_runs = sys.argv[1]
  crawler = Crawler(num_of_runs)
  crawler.run()

Then, I run it this way:

python path/crawler.py 10

From my understanding, it should loop 10 times and stop, right? Why it doesn't?

like image 515
bmpasini Avatar asked Apr 30 '15 14:04

bmpasini


People also ask

Why does my while loop not stop?

Why can't my program get out of a while loop? Simply because it isn't meeting the exit conditions. The expectation would be that the loop will execute 100 times but because counter is an unsigned 8 bit type it has a range 0–255 and when decremented from zero goes to 255 causing an infinite loop.

How do I make a while loop stop?

Breaking Out of While Loops. To break out of a while loop, you can use the endloop, continue, resume, or return statement. endwhile; If the name is empty, the other statements are not executed in that pass through the loop, and the entire loop is closed.

How do you stop an endless while loop?

An infinite loop is a loop that runs indefinitely and it only stops with external intervention or when a break statement is found. You can stop an infinite loop with CTRL + C . You can generate an infinite loop intentionally with while True . The break statement can be used to stop a while loop immediately.

Why is my while loop running infinitely?

Basically, the infinite loop happens when the condition in the while loop always evaluates to true. This can happen when the variables within the loop aren't updated correctly, or aren't updated at all.


2 Answers

num_of_runs = sys.argv[1]

num_of_runs is a string at that stage.

while self.run_number <= self.num_of_runs:

You are comparing a string and an int here.

A simple way to fix this is to convert it to an int

num_of_runs = int(sysargv[1])

Another way to deal with this is to use argparser.

import argparse

parser = argparse.ArgumentParser(description='The program does bla and bla')
parser.add_argument(
    'my_int',
    type=int,
    help='an integer for the script'
)

args = parser.parse_args()
print args.my_int
print type(args.my_int)

Now if you execute the script like this:

./my_script.py 20

The output is:

20

Using argparser also gives you the -h option by default:

python my_script.py -h
usage: i.py [-h] my_int

The program does bla and bla

positional arguments:
  my_int      an integer for the script

optional arguments:
  -h, --help  show this help message and exit

For more information, have a look at the argparser documentation.

Note: The code I have used is from the argparser documentation, but has been slightly modified.

like image 157
Paco Avatar answered Oct 19 '22 23:10

Paco


When accepting input from the command line, data is passed as a string. You need to convert this value to an int before you pass it to your Crawler class:

num_of_runs = int(sys.argv[1])

You can also utilize this to determine if the input is valid. If it doesn't convert to an int, it will throw an error.

like image 41
Andy Avatar answered Oct 20 '22 00:10

Andy