I have a piece of code in Pycharm , and I want to loop just a part of it, not with the beginning part where are located the login info. Let's say that I want to loop from the 44 line to 99 and only for 20 loops.

  • Hi Alex Alex. It's important for the replies to receive an "accepted" flag is you find them well-completed. If you find some of the answers give you the idea you were looking for, I'd suggest to mark it as accepted. – João Farias Mar 24 '19 at 19:19


start = 1
multiply = start * 2
divided = multiply / 3

Now, let's say you want to multiply by two 20 times:

start = 1
multiply = multiply_value(value = start, number_of_times = 20)
divided = multiply / 3

def multiply_value(value, number_of_times):
   if(number_of_times <= 0):
      return value
      return multiply_value(value * 2, number_of_times - 1)

Alternatively, you could use a for loop:

def multiply_value(value, number_of_times):
       result = 1
       for i range(0, number_of_times):
           result = result * 2
       return result

I suggest reading the section 4 of this Learn X in Y Minutes page.


Loops are well documented: https://wiki.python.org/moin/ForLoop

for x in range(0, 3):
    print "We're on time %d" % (x)

Suggest you do some reading on Python fundamentals:

Not the answer you're looking for? Browse other questions tagged or ask your own question.