Ex35 bear_moved

Refering to the script of ex35 from the book in line 34 we say the bear_moved = True. When the while loop completes the value of bear_moved is reset to line24 – bear_moved = False. (that’s what I studied in C about while loops). (I was told that a value assigned under a loop will stay under the loop and will be nothing outside the loop) my question - is that not the case in Python? A variable willl retain the value even if it is assigned under loop and will not reset?
please explain in an easy way,
cheers
-Dev

No, in Python (and in C for that matter) changes to variables that have been defined outside the loop carry through outside the loop.

The only difference is that in Python even variables defined in loops are available later outside the loop.

I don’t have your code in front of me now, but I’m pretty sure bear_moved is not reset.

2 Likes

Alright. Thank you florian.

1 Like