As with the print statement, it's pretty clear that I can access this variable, and I know I cannot modify global variables in a function, but do loops only work for local variables, which seems to be the case. Can anyone please clarify on variable scopes when working with loops in python. I know I can achieve the intended functionality by declaring the variable in function itself, and I know the loop is an infinite loop, I just want to understand why we can't do this (or precisely what more rules are there regarding scopes and namespaces in loops inside function) I couldn't find this.
def racing_result():
print(race_on)
while race_on:
for colour,turtle in turtles.items():
if turtle.xcor() > 230:
winner_colour = turtle.pencolor()
print(winner_colour)
turtle.forward(random.randint(0,10))
return winner_colour
if usr_bet:
race_on = True
colour = racing_result()