Feb-26-2021, 10:14 PM
You mentioned that this was a simplified example. If you really need to do this, the common way is using Sentinel values to signal something. It's a value that simply cannot be generated any other way. Here's an example:
>>> def outer(): ... class Sentinel: ... pass ... def inner(): ... question = input("Answer here: ") ... if question == "q": ... return Sentinel() ... return question ... x = inner() ... if isinstance(x, Sentinel): ... return ... while True: ... print(f"do something with {x}") ... ... >>> def menu(): ... print("hi i'm a menu") ... running = True ... while running: ... choice = input("Choose: ") ... if choice == "1": ... outer() ... else: ... running = False ... >>> menu() hi i'm a menu Choose: 1 Answer here: q Choose: 1 Answer here: q Choose: 1 Answer here: q Choose: asdfThe other option is to include state with the function return. Something like:
>>> def outer(): ... def inner(): ... question = input("Answer: ") ... state = {"response": question} ... state["next"] = "return" if "q" == question else "work" ... return state ... x = inner() ... if "return" == x["next"]: ... return ... while True: ... print(f"do something with {x}") ...That version can quickly get very messy if you're not careful, though, and make it very difficult to debug. But the state method is how things like level transitions work in video games.