0

having trouble getting this function to work:

go_on = False
def keep_going():
    further = input("input: ")
    if further == "n":
        go_on = True
    else:
        go_on = False
    return go_on

while go_on == False:
    keep_going()

The idea being, that if you put in 'n', the while loop gets interrupted, obviously
But even if the function returns True after the input is 'n', the name go_on keeps switching to False.

vi_O.o
  • 3
  • 2
  • 1
    The function’s return is not being assigned to anything. That aside, this setup has several flaws; suggesting there is a deeper issue here which is not described in the question. – S3DEV Aug 27 '21 at 15:17
  • There are two different `go_on` variables in your code. Assigning to the local variable `go_on` in `keep_going` doesn't change the value of the global variable `go_on`. – Brian61354270 Aug 27 '21 at 15:18
  • Would https://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python answer your question? – Bill Lynch Aug 27 '21 at 15:18
  • @Brian - I've edited the two different ones for clarification - thought it didn't matter to use the same name, since they are in fact different anyway. – vi_O.o Aug 27 '21 at 15:31

2 Answers2

2

You need to actually assign the returned the value of keep_going to be used by the loop

go_on = False
def keep_going():
    go_on = input("input: ")
    if go_on == "n":
        go_on = True
    else:
        go_on = False
    return go_on

while go_on == False:
    go_on = keep_going()

It's also important to understand why this is needed. The go_on declared on line 1 isn't scoped to any function where as the go_on assigned with input is locally scoped to the function keep_going. It's important to understand that these are two different variables. When using go_on inside of the function the scope will default to that of the function. This means your assignments and return statement only edit the local variable and not the global like you may expect.

BTables
  • 4,413
  • 2
  • 11
  • 30
  • is is worth explaining why the `go_on` in the method isn't the same `go_on` as at the top of the script? Seems like there's also some variable scope confusion – Basic Aug 27 '21 at 15:27
  • 1
    @Basic that's a very good point. I edited my comment to add a little extra context. – BTables Aug 27 '21 at 15:32
  • a simple `global go_on` inside the function seems to work then – vi_O.o Aug 27 '21 at 15:43
  • @gurkenmeister you are correct that a single global `go_on` will work. That being said, if you went that route you should not return anything from `keep_going` or assign the result in your loop. – BTables Aug 27 '21 at 15:45
2

The other answer identifies why your code isn't working as expected, but it's worth noting you could simplify this entire process down to:

while input("input: ") != "n":
    pass
Basic
  • 26,321
  • 24
  • 115
  • 201
  • Thx for the info - trying to call the function in another program to end the program loop. Got to check that out, if it's usable instead. – vi_O.o Aug 27 '21 at 15:42
  • It sounds like you have an [XY problem](https://en.wikipedia.org/wiki/XY_problem). It might be worth asking another question with a larger context. It's quit uncommon to call functions in another process. Python multiprocessing supports it, but it's for rather esoteric use-cases and unlikely to be what you actually need. Do you want 2 processes to exchange data? Or do you want one process to control the flow of another? Or monitor execution and recover? Or ... You get the picture, and they all have their own solutions. – Basic Aug 27 '21 at 21:34
  • Just playing around with code, trying to learn something ... – vi_O.o Aug 30 '21 at 06:48