0

I have been trying to make a code that requires modification of a variable that is inside a loop.

a = 1
b = 2
while a<b:
    print(b)

now I want to change the value of b while the loop is running. I have tried to find an answer but could not do so. Can it be possible that I use this loop with an initial value of b and terminate this loop automatically and start a new one when variable b is modified to something else? How would that be possible? Note: I want to modify the var b from outside.

Further Explanation:

The code should be designed in such a way that a variable used to check the condition of the while loop can be modified when the loop is already running. Let's say a user wants to use this while loop as a function, he passes variables in the function to start the loop. Now he wants to change the variable value (in the above case var b), which is used in the while loop condition.

def start_loop(a,b):
   while a<b:
     #more codes will be included here
     print('The loop is running infinitely')



#call the function 
 start_loop(1,2) 
 Consol: The loop is running infinitely....

The loop is running infinitely since the loop is already running I can not ask the while loop to change the value of var b. I am looking for a solution that can handle this situation. How a user can modify "var b" from outside as the loop is already running?

Or how can I call the same start_loop() function with the modified value of var b? Doing so should result in the termination of the older loop and the start of this new loop.

I hope I'll find the answer here :) Thanks.

5
  • 1
    What should your code actually do? Commented May 7, 2021 at 12:12
  • I'm so confused. You can straight up modify the value of b in the while block as is, what's stopping you? "modify the var b from outside". Outside of what? Why is the global-variables tag relevant? I think if you post a more inclusive code snippet it would be enlightening. Commented May 7, 2021 at 12:15
  • @Reti43 the code should be designed in such a way that a variable used to check the condition of the while loop can be modified when the loop is already running. Let's say a user wants to use this while loop as a function, he passed variables in the function to start the loop. now he wants to change the variable value (in the above case var b), which is used in the while loop condition. def start_loop(a,b): while a<b: # more codes will be included here print('The loop is running infinitely ') Commented May 7, 2021 at 13:31
  • @Reti43 # call the function start_loop(1,2) Consol: The loop is running infinitely since the loop is already running I can not ask the while loop to change the value of var b. I am looking for a solution that can handle this situation. How will a user modify 'b' from outside as the loop is already running? Or how can I call the same start_loop() function with the modified value of var b? Doing so would result in the termination of the older loop and the start of this new loop. I put global var as a tag because I thought it could be solved by global vars if possible. Commented May 7, 2021 at 13:31
  • It sounds like you want your function start_loop to run in parallel while you also run more code outside the function. The way you have it, the while loop will block everything else because it will run indefinitely. Also, the b in the function is not the same as the b in the outer scope. You can modify it inside the function without affecting the other one. What exactly are you trying to do? Commented May 7, 2021 at 13:59

1 Answer 1

1

If you want to do what you said at the same time as running the loop, maybe the content of this link will help you pytho.org. If the solution is published, visit these links: eval(), exec()

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

1 Comment

Thank you Delta, I have just tried some codes from the link you have given, and I think this would work. I am gonna explore more about this coroutine of python.

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.