Answer the question
In order to leave comments, you need to log in
Bug in Python?
Hello, when the code is executed everything is fine
length = 100
def len():
while True:
if length == 0:
break
print(length)
len()
length = 100
def len():
while True:
length -= 1
if length == 0:
break
print(length)
len()
Answer the question
In order to leave comments, you need to log in
Python uses a simple rule: if there is an assignment inside the function, then the variable is considered local. That is why the comparison uses a global variable, and in the case of a decrease by one, the interpreter considers the variable as local and does not find its initialization.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question