(Nov-07-2023, 07:08 AM)Iqratech Wrote: # The loop_variable is only accessible within this while loopThis is simply NOT true. The name (variable) continues to live and be accessible after you exit the loop. The loops don't create own scope
# The loop_variable is not accessible here and will raise an error if you try to access it
In this code snippet, loop_variable is only accessible within the while loop's scope, and you won't be able to access it outside of the loop.
Keep in mind that the scope of the variable is limited to the block where it is defined. If you need to use the variable's value outside of the loop, you should declare it before the loop and update its value within the loop
1 2 3 4 |
while True : spam = 42 break print (spam) |
Output:42
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs