1

I am a beginner in python programming and I encountered an issue with my code.

When a user types an invalid operation, it notifies the users but exits the program (line 33). How can I get it to ask user to enter a math operation again?

#This python program calculates the sum, difference, product, or quotient of two numbers defined by users.

#Define add function and return the result of num1 + num2
def add(num1, num2):
    return num1 + num2

#Define subract function and return the result of subtracting num1 - num2
def sub(num1, num2):
    return num1 - num2

#Define multiplication function and return the result of multiplying num1 * num2
def mul(num1, num2):
    return num1 * num2

#Define division function and return the result of dividing num1 / num2
def div(num1, num2):
    return num1 / num2


#Define main purpose/function of the program
def main():

    #Ask what math operation to perform
    operation = input("What do you want to do? (+, -, *, /): ")

    #If the operation is not +, -, *, or /, invalid operation
    if(operation != '+' and operation != '-' and operation != '*' and operation != '/'):
        print("You must enter a valid operation!")

    #If valid, perform specified operation
    else:
        var1 = int(input("Enter num1: "))
        var2 = int(input("Enter num2: "))
        if(operation == '+'):
            print(add(var1, var2))
        elif(operation == '/'):
            print(div(var1, var2))
        elif(operation == '-'):
            print(sub(var1, var2))
        else:
            print(mul(var1, var2))

main()
jayteezer
  • 115
  • 2
  • 13
  • 1
    You can put `main` in a loop as follows: `while 1: main()` – Ozgur Vatansever Oct 18 '15 at 07:44
  • You might want to use while loop for the first operation input instead of `if ... else`. Your operation and var1,var2 are in sequence. Not like you need one or another. That is why the program exits when you choose the invalid operation. – Aung Oct 18 '15 at 07:57
  • Take a look at [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/4014959), which will also show you how to handle invalid numbers being input. – PM 2Ring Oct 18 '15 at 08:21

2 Answers2

1

Simply ask the user to input it again:

#This python program calculates the sum, difference, product, or quotient of two numbers defined by users.

#Define add function and return the result of num1 + num2
def add(num1, num2):
    return num1 + num2

#Define subract function and return the result of subtracting num1 - num2
def sub(num1, num2):
    return num1 - num2

#Define multiplication function and return the result of multiplying num1 * num2
def mul(num1, num2):
    return num1 * num2

#Define division function and return the result of dividing num1 / num2
def div(num1, num2):
    return num1 / num2


#Define main purpose/function of the program
def main():

    #Ask what math operation to perform
    operation = input("What do you want to do? (+, -, *, /): ")

    #If the operation is not +, -, *, or /, invalid operation
    while (operation != '+' and operation != '-' and operation != '*' and operation != '/'):
        print("You must enter a valid operation!")
        operation = input("What do you want to do? (+, -, *, /): ")

    var1 = int(input("Enter num1: "))
    var2 = int(input("Enter num2: "))
    if(operation == '+'):
        print(add(var1, var2))
    elif(operation == '/'):
        print(div(var1, var2))
    elif(operation == '-'):
        print(sub(var1, var2))
    else:
        print(mul(var1, var2))
 main()
lingxiao
  • 1,214
  • 17
  • 33
0

In case you are using Python2, you can't use input here because input() evaluates the input in the execution context. So, you should be using raw_input(). In case of Python-3.x, you can use input().

As far as your question is concerned, you can put it into a while loop.

#This python program calculates the sum, difference, product, or quotient of two numbers defined by users.

#Define add function and return the result of num1 + num2
def add(num1, num2):
    return num1 + num2

#Define subract function and return the result of subtracting num1 - num2
def sub(num1, num2):
    return num1 - num2

#Define multiplication function and return the result of multiplying num1 * num2
def mul(num1, num2):
    return num1 * num2

#Define division function and return the result of dividing num1 / num2
def div(num1, num2):
    return num1 / num2


#Define main purpose/function of the program
def main():

    while True:
      #Ask what math operation to perform
      operation = raw_input("What do you want to do? (+, -, *, /): ")
      print "operation is ", operation, type(operation)

      #If the operation is not +, -, *, or /, invalid operation
      if operation != '+' and operation != '-' and operation != '*' and operation != '/':
        print("You must enter a valid operation!")
      #If valid, perform specified operation
      else:
        var1 = int(input("Enter num1: "))
        var2 = int(input("Enter num2: "))
        if(operation == '+'):
            print(add(var1, var2))
        elif(operation == '/'):
            print(div(var1, var2))
        elif(operation == '-'):
            print(sub(var1, var2))
        else:
            print(mul(var1, var2))
        return 0

main()
blackmamba
  • 1,952
  • 11
  • 34
  • 59
  • If the OP is using Python 3 they can't use `raw_input()`. The old evil `input()` from Python 2 has been eliminated in Python 3 and `raw_input()` has been renamed to `input()`. – PM 2Ring Oct 18 '15 at 08:19
  • OP didn't mention which python version is being used. If it is Python-3 then input() will work else raw_input has to be used :) – blackmamba Oct 18 '15 at 08:20
  • 1
    Sure. It's annoying when people don't mention the version they're using when we need to know that info to write a good answer. OTOH, new Python programmers may not realize when that info is important. But if the OP's using `input()` and `print()` then it's fair to assume that they're using Python 3, IMHO. – PM 2Ring Oct 18 '15 at 08:25