Reputation: 115
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()
Upvotes: 1
Views: 360
Reputation: 1224
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()
Upvotes: 1
Reputation: 2002
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()
Upvotes: 0