0

I am creating a small command line calculator which takes inputs from the user and outputs the answer. There are a couple "commands" that the user can use, such as "add" and "minus". The main loop looks like this:

on = True
while on:
  in = input("Type your command -> ")
  if(in == "add")
    n1 = input("What is the first number -> ")
    n2 = input("What is the second number -> ")
    print("{} + {} = {}".format(n1, n2, n1 + n2))
  ...
  else:
    print("Please enter a valid operation.")

I am looking to add a "command" cancel which would cancel any command the user is currently doing. For example, if they are doing addition and they enter the input "cancel" then it goes back into the main loop. How could I do this?

3
  • You would need to manually check and handle that case. You can use continue though to skip the rest of the current iteration and start back at the top of the loop. Commented Oct 26, 2020 at 17:07
  • You should also use int. n1 = int(input("What is the first number -> ")) because you're using mathematic operations Commented Oct 26, 2020 at 17:11
  • 1
    Your question should remain strictly a question. You are more than welcome to post an answer of your own, though (and even accept it if you like). Commented Oct 26, 2020 at 18:14

3 Answers 3

1

A good way to handle nonlocal exits is with an exception.

class CancelException(Exception):
    "Exception raised when the calculator user presses C"
    pass


def getNumber(prompt):
    number = input(prompt)
    if number in ("C", "cancel"):
        raise CancelException("User selected cancel")
    return float(number)


on = True
while on:
    try:
        in = input("Type your command -> ")
        if(in == "add")
            n1 = get_number("What is the first number -> ")
            n2 = get_number("What is the second number -> ")
            print("{} + {} = {}".format(n1, n2, n1 + n2))
        ...
        else:
            print("Please enter a valid operation.")
    except CancelException:
        pass

Once you start playing with this, you'll realize that you also need to catch an exception when you try to convert input which isn't a number into a number. That will actually be quite similar to the cancel scenario.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe try something like:

if(in == "add")
   n1 = input("What is the first number -> ")
   n2 = input("What is the second number -> ")
   if(n1 == "cancel" or n2 == "cancel")
      print("cancelling")
   else:    
     print("{} + {} = {}".format(n1, n2, n1 + n2))

1 Comment

I was thinking of something like this, but this would only cancel the command if both n1 and n2 were entered, so if the user cancels when inputting the first number they would still be prompted for the second one.
0

you can take another input from the user. add these lines in at the end of your loop:

'''    
    ask = input('do you want to continue? (yes / no):')

    if ask == 'yes':
        on = True
    else:
        on = False

'''

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.