Hi, I am new to phyton and now I have some problems in terminating my while loop.
This loop runs infinitely, I tried to print both count and N and yes, the value of count does go way beyond N but the loop is never terminated. I am guessing this is because I am getting N from command line and count is initialized inside the program so their 'type' does not match.

Any ideas how to terminate this loop?

if len(sys.argv) == 3 :
    infile = open(sys.argv[2])
    N = sys.argv[1]
    count = 0
    while count < N:
        s = infile.readline()
        print s
        count = count + 1
    infile.close()

Thank you

Change N = sys.argv[1] to N = int(sys.argv[1]) .

Otherwise you're comparing a number to a string, since that's what sys.argv[-] returns.

Of course, then you've probably got to add an exception handler for when sys.argv[1] isn't convertible to a number, but that's for later...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.