1

I've created a basic, multiple choice, interactive calculator in Python. I want the user to be able to have the option to stop the programme running by answering "No" when asked whether they want to test out my calculator.

I want to be able to print("Ok, no problem") which you can see is already there but I need something extra to stop the programme running if this is the answer that the user picks.

Code is below. See lines 12-13.

    name = input("Hi There. What is your name? ")
    print("Well Hi " + name + ", it sure is nice to meet you")
    age = input("So how old are you anyway? ")
    print("Wow, you're " + age + " huh? ")

    print(name + " I would like you to try out the companies new calculator. Would you be 
    happy to do that? ")
    answer = input("Please answer Yes or No ")

    if answer == "Yes":
        print("Thank you, I appreciated that. Let's begin")

    elif answer == "No":
        print("Ok, no problem")

    else:
        print("Sorry I didn't quite get that. Please answer yes or no")

    import math
    number_1 = int(input("Please pick a number "))
    order = (input("Ok, now please pick either: +, -, / or * "))
    number_2 = int(input("Great, now please pick a second number "))

    if order == "+":
        print(number_1 + number_2)
    elif order == "-":
        print(number_1 - number_2)
    elif order == "/":
        print(number_1 / number_2)
    elif order == "*":
        print(number_1 * number_2)
    else:
        print("Sorry that is not +, -, / or *. Please enter a relevant order")

Any help that you can give me would be much appreciated.

theduck
  • 2,589
  • 13
  • 17
  • 23

2 Answers2

-1

You can use sys.exit to terminate the program:

import sys

#...

elif answer == "No":
    print("Ok, no problem")
    sys.exit()
Anis R.
  • 6,656
  • 2
  • 15
  • 37
  • Glad this hepls! If this answered your question, please consider upvoting or accepting this answer. Have a good day! – Anis R. Jan 12 '20 at 14:38
-1

Here is an answer for you which helped me out:

Let me give some information on them:

quit() raises the SystemExit exception behind the scenes.

Furthermore, if you print it, it will give a message:

>>> print (quit) 
Use quit() or Ctrl-Z plus Return to exit 
>>>

This functionality was included to help people who do not know Python. After all, one of the most likely things a newbie will try to exit Python is typing in quit.

Nevertheless, quit should not be used in production code. This is because it only works if the site module is loaded. Instead, this function should only be used in the interpreter.

exit() is an alias for quit (or vice-versa). They exist together simply to make Python more user-friendly.

Furthermore, it too gives a message when printed:

>>> print (exit) 
Use exit() or Ctrl-Z plus Return to exit
>>>

However, like quit, exit is considered bad to use in production code and should be reserved for use in the interpreter. This is because it too relies on the site module.

sys.exit() raises the SystemExit exception in the background. This means that it is the same as quit and exit in that respect.

Unlike those two however, sys.exit is considered good to use in production code. This is because the sys module will always be there.

os._exit() exits the program without calling cleanup handlers, flushing stdio buffers, etc. Thus, it is not a standard way to exit and should only be used in special cases. The most common of these is in the child process(es) created by os.fork.

Note that, of the four methods given, only this one is unique in what it does.

Summed up, all four methods exit the program. However, the first two are considered bad to use in production code and the last is a non-standard, dirty way that is only used in special scenarios. So, if you want to exit a program normally, go with the third method: sys.exit.

Or, even better in my opinion, you can just do directly what sys.exit does behind the scenes and run:

raise SystemExit

This way, you do not need to import sys first.

However, this choice is simply one on style and is purely up to you.

High-Octane
  • 1,104
  • 5
  • 19
  • Thanks Man, lots of different scenarios to think about. raise SystemExit is a pretty neat way to do it, like you say because you dont have to import sys. I've applied this to my script. Thanks for your help. – David Mason Jan 12 '20 at 14:34
  • Did you just copy/paste this post ?! https://stackoverflow.com/a/19747562/7692859 – Anis R. Jan 12 '20 at 14:41
  • @AnisR. It's there in the title. Did you invent your coding or learn it? – High-Octane Jan 12 '20 at 14:42
  • Consider adding some credit to the original answer, by at least adding a link to it. – Anis R. Jan 12 '20 at 14:44