Hi all, just wondering if someone could help me out with a small loop question.

If I had an input field in a GUI which writes to variable "counter" (a numerical value), how would I be able to use this value to control the amount of times a loop is run. I have tried using the break function to exit a loop when it reaches a certain number of iterations, but it nothing I've attempted seems to work.

Any help is appreciated

Do you have any code to show? Or else, what kind of loop do you use? I'm guessing that it's a while True: loop due to the need of using break.

If this is the case then using a for i in xrange(counter): loop would be more recommended.

Remember that if you break while in a loop that is inside the loop that you want to break, you only break out of the inner loop, but not the outer loop.

Cheers!

Edited 3 Years Ago by lim.charmender: Wrong language examples

Thanks for the replies:D
at the moment the code section looks like this:

        URL = (self.link_entry.get())
        HITS = (self.hits_entry.get())
        DELAY = (self.delay_entry.get())
        COUNTER = int(HITS)
        WAIT = int(DELAY)



        for i in xrange(COUNTER):
            time.sleep(WAIT)
            webbrowser.open(URL, new=0, autoraise=True)

First I tried just using the variable 'hits', but I noticed it must first be converted to an integer, so I used the counter variable. Finally works!

Thanks again for the help guys:D

This would be much simpler without the 'fake constant named all uppercase variables'

        for i in xrange(int(self.hits_entry.get())):
            time.sleep(int(self.delay_entry.get()))
            webbrowser.open(self.link_entry.get(), new=0, autoraise=True)

Edited 3 Years Ago by pyTony

Comments
If it isn't used in multiple places, sometimes constants just make the code longer! :D

Haha that really is a lot simpler, looks like I've got a looking way to go yet:)
Cheers guys:)

This question has already been answered. Start a new discussion instead.