I want to know if is it possible to change the value of the iterator in its for-loop?
For example I want to write a program to calculate prime factor of a number in the below way :
def primeFactors(number):
for i in range(2,number+1):
if (number%i==0)
print(i,end=',')
number=number/i
i=i-1 #to check that factor again!
My question : Is it possible to change the last two line in a way that when I change i
and number
in the if
block, their value change in the for
loop!
Update: Defining the iterator as a global
variable, could help me? Why?
number
it does not change the value here:range(2,number+1)
because this is an expression that has already been evaluated and has returned a list of numbers which is being looped overcontinue with
statement to allow injection back into the iterator, but it is fairly simple to code a custom wrapper around an iterator that allows such behavior already.