1

I want to have a Python program like--

for i in range(r):
    if (i==2):
        #change r in some way

which will run the loop for the new range r, after it gets modified in the if statement.

Even if I change r after the if statement,the for loop runs for the initial r I gave.This must be happening because range(r) gets fixed in the for statement in the first line itself,and is not affected by change in r later on.

Is there a "simple way" to bypass this?

By "simple" I mean that I don't want to add a counter which counts how many times the loop already ran and how many times it need to run again after changing(specifically increasing) r,or by replacing for loop with a while loop.

6
  • 2
    using a while loop and manual counter is he way to go. you cannot change r Commented May 10, 2020 at 7:38
  • Remember: arguments are passed by value (see “call by object sharing”) in Python. The function is given the current value of r (that is r is evaluated as an expression immediately), and otherwise has no association with the variable use as an argument. Commented May 10, 2020 at 7:42
  • Instead of using a while loop as the others are saying, I think that there is very likely a better option solving this. Could you extend your post by showing us what it is that you want to achieve? Maybe python indexing can already help you? >>> x = [1,2,3] >>> x[2] = 5 >>> x [1, 2, 5] Commented May 10, 2020 at 7:46
  • 1
    If one wanted to write some complicated code for such a general request, instead of a simple counter, it might look like this: r = 3; g = my_generator(r); for i in g; if i == 2: g.set_max(42). Where my_generator uses properties to maintain state. Again, and to solidify a point, r was never passed to the my_generator function/constructor: the value 3 was. Commented May 10, 2020 at 7:46
  • 1
    @Dremet I was writing a code for random walk problem without repitition,so i need to check whether the new point was ever visited before or not.If it was visited then,we need to reject that point right?So that step never occured,but initially I started out with a fixed number of steps,so every time I dont use a point,i dont want i to increase at all. Adding the main code may annoy some users,as it will neither match the title,nor the tags(it will become a scientific computing question).and may also lead to closing of the question as "offtopic". Commented May 10, 2020 at 8:26

2 Answers 2

1

When you say:

for i in range(r):

range(r) creates a range object. It does this only once when the loop is set up initially. Therefore, any changes you make to r inside the loop have no effect on the performance of the loop, since it's the range object that dictates the number of iterations (it just happens to be initialized with r).

Rule of thumb: If you know how many iterations you need in advance, use a for loop. If you don't know how many iterations you need, use a while loop.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks,but I already wrote almost everything in the question,u told in this answer.
I think I should really have to use a while statement anyway.
1

I don't believe this is possible. However, using a while loop and a manual counter is itself an extremely simple way to do this.

The code will look something like this:

i = 0
while i < r:
    if i == 2:
        # Change r in some way

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.