0

Python beginner here. Sorry if it's a basic python concept

over = False

def run():
    user_input = input("Over? (y/n): ")
    if(user_input == 'y'):
        over = True

while not over:
    run()

Although the input is 'y' the loop doesn't stop.

Aditya
  • 757
  • 1
  • 12
  • 23
  • 2
    Please read python scoping rules here -> http://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules – JkShaw Apr 29 '17 at 16:47

3 Answers3

1

You need to write global over, so function run() will change global variable

over = False

def run():
    global over
    user_input = input("Over? (y/n): ")
    if(user_input == 'y'):
        over = True

while not over:
    run()
Sklert
  • 242
  • 5
  • 12
1

You shouldn't be using a global variable here. Return a boolean, and call run as the condition of the loop. (At this point, you may want to reconsider the name run as well.)

def run():
    user_input = input("Over? (y/n)")
    return user_input == 'y'

while run():
    ...
chepner
  • 497,756
  • 71
  • 530
  • 681
0

You are setting the local variable over inside the function run(), but you aren't passing it out to the scope from which it was called. Instead return the value to the calling scope like this:

over = False

def run():
    user_input = input("Over? (y/n): ")
    if(user_input == 'y'):
        over = True
    else:
        over = False
    return over

while not over:
    over = run()
Craig
  • 4,605
  • 1
  • 18
  • 28