1

I am attempting to exit a program without using sys.exit() The user is asked whether they wish to continue and if they input "Yes" a message saying so is printed and the program continues to run. If they input anything else a message saying they chose to exit is printed and then the program is meant to exit.

def keep_going():

    answer = raw_input("Do you wish to continue?")

    if answer == "yes":
        print "You have chosen to continue on"
    else:
        print "You have chosen to quit this program"

What I am struggling with is what to add to ELSE to return something to my main which will cause the program to exit and how to go about writing that in code.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Clair Davis
  • 37
  • 1
  • 1
  • 3
  • If I may ask, what's wrong with using `sys.exit()`? – DigitalDouble May 02 '15 at 10:51
  • The assignment brief stipulates that we are not allowed to use it, unfortunately. Instead I was told to return something to main which will take me to the end of the set of statements to then allow the program to exit. Not sure how, is it possible to call a function within an IF statement? For example, if i created a function called exit and placed it at the end of my program, am I able to call it eg. ELSE: print :You have chosen to quit the program" Call exit ?? – Clair Davis May 03 '15 at 07:41
  • If any of the answers here helped you, you should up-vote them by clicking the up-arrow to the left of each answer. If any of the answers solved your problem you should select it by clicking the check-mark next to it. This will help future visitors that have the same problem. – thebjorn May 05 '15 at 03:09
  • Does this answer your question? [How do I get a result (output) from a function? How can I use the result later?](https://stackoverflow.com/questions/3052793/how-do-i-get-a-result-output-from-a-function-how-can-i-use-the-result-later) – Karl Knechtel Jan 09 '23 at 23:11
  • "what to add to ELSE to return something to my main" - how about, a `return` statement, that specifies a value? "which will cause the program to exit" - so, in the place where the function is called, put code that **checks** the return value, and exits if appropriate? – Karl Knechtel Jan 09 '23 at 23:13

4 Answers4

1

You can try this

def keep_going():

    answer = raw_input("Do you wish to continue?")

    if answer == "yes":
        print "You have chosen to continue on"
    else:
        print "You have chosen to quit this program"
        quit()
snow
  • 55
  • 1
  • 1
  • 11
0

Just, return something, and if that is returned, then let your main function exit, either by falling off the end, by using a return statement, or calling sys.exit()/raise SystemExit.

As an example, I'm here returning a string (a different one based on what the user answered):

def keep_going():
    answer = raw_input("Do you wish to continue?")
    if answer == "yes":
        print "You have chosen to continue on"
        return "keep going"
    else:
        print "You have chosen to quit this program"
        return "please exit"

Now, in main, I can test which of these strings keep_going() returned:

def main():
    while keep_going() != 'please exit':
        # your code here

if __name__ == "__main__":
    main()

While strings will work for this purpose, other values are more commonly used for such a task. If keep_going() returned True (instead of "keep going") or False (instead of "please exit"), then main could be written like

def main():
    while keep_going():
        # your code here

This also reads pretty naturally ("while keep going do your code"). Note that in this case I'm not comparing the return value to something since True and False are truthy variables - and Python's branching control structures (like if and while) know how they work, i.e. there is no need to write keep_going() == True, and indeed it is considered un-pythonic to do so.

thebjorn
  • 26,297
  • 11
  • 96
  • 138
  • The assignment brief stipulates that we are not allowed to use it, unfortunately. Instead I was told to return something to main which will take me to the end of the set of statements to then allow the program to exit. Not sure how, is it possible to call a function within an IF statement? For example, if i created a function called exit and placed it at the end of my program, am I able to call it eg. ELSE: print :You have chosen to quit the program" Call exit ? Thanks for your reply, also – Clair Davis May 03 '15 at 07:50
  • I have updated my answer, although it feels like you might have a more fundamental disconnect here.. If this still doesn't click for you then please edit your question to include your `main()` function. – thebjorn May 03 '15 at 19:49
0

If you are so much keen about not using sys.exit() you could directly use raise SystemExit. Well this exception is technically raised when you call sys.exit() explicitly. In this way you don't need to import sys at all.

def keep_going():

  answer = raw_input("Do you wish to continue?")

  if (answer == "yes"):
       print ("You have chosen to continue on")
  else:
    print "You have chosen to quit this program"
    raise SystemExit

This answer will give you the alternate possible ways.

Community
  • 1
  • 1
no coder
  • 2,290
  • 16
  • 18
0

Try this:

def main():
    while keep_going():
        keep_going()

def keep_going():

    answer = raw_input("Do you wish to continue?")

    if answer == "yes":
        print "You have chosen to continue on"
        return True
    else:
        print "You have chosen to quit this program"
        return False

if __name__ == "__main__":
    main()

The program will continue calling keep_going() as long as it returns true, that is when a user answers "yes"

An even shorter solution would be to call keep_going() after the "yes" condition:

def keep_going():

    answer = raw_input("Do you wish to continue?")

    if answer == "yes":
        print "You have chosen to continue on"
        keep_going()
    else:
        print "You have chosen to quit this program"
DigitalDouble
  • 1,747
  • 14
  • 26