Since this looks like homework, I doubt the OP is allowed to use the typical ways to solve the problem. I think this is an exercise in input validation and string manipulation; followed by program flow and understanding function return values.
There are two things you need to do here:
- Figure out what would be valid inputs to your program.
- Keep prompting the user till he or she enters input that is valid for your program.
For #1, we know that valid inputs are numbers (positive or negative integers), and they must be in the form of an expression. So this means, the minimum length of the input will be three (two numbers and a math symbol) and characters (strings) in the input are not valid.
This is our basic loop to get the user's input:
expression = raw_input('Please enter the expression: ')
expression_result = check_input(expression)
while not expression_result:
print 'You did not enter a valid expression'
expression = raw_input('Please enter the expression: ')
expression_result = check_input(expression)
The check_input
method will validate whatever the user entered is accurate based on our rules:
def check_input(input_string):
# Check the basics
if len(input_string) < 3:
return False
# Check if we are getting passed correct characters
for character in input_string:
if character not in '1234567890' or character not in '/*+-':
return False
# Things like /23 are not valid
if input_string[0] in '/*+':
return False
return input_string
After you have the correct input, the next step is to split the input into the various parts that you need to feed to your math functions. I'll leave that part up to you.
Assuming you have the correct string (that is, it is valid input for your program), you now need to split it into two parts.
- The operator (the math symbol)
- The operands (the numbers surrounding the math symbol)
So we know that we have a limited set of operators +,-,/,*
, so one idea is to use the split()
method of strings. This works well:
>>> s = '4+5'
>>> s.split('+')
['4', '5']
You would try splitting the string with all of your operators and then check the results. Note that splitting a string with a character that doesn't exist won't raise any errors, but you'll just the string back:
>>> s = '4+5'
>>> s.split('/')
['4+5']
So one approach is - split the string on the operators, if the resulting list has length > 2, you know that the first member of the resulting list is the left hand side of the operator, and the second member of the list is whatever is on the right hand side.
This works fine with positive numbers, with negative numbers however:
>>> s = '-4+3'
>>> s.split('-')
['', '4+3']
Good news is we aren't the first ones to reach this problem. There is another way to evaluate equations, called the Polish notation (also called prefix notation). Here's the algorithm from the wikipedia page:
Scan the given prefix expression from right to left
for each symbol
{
if operand then
push onto stack
if operator then
{
operand1=pop stack
operand2=pop stack
compute operand1 operator operand2
push result onto stack
}
}
return top of stack as result
To get a normal expression (called infix) to the polish flavor, use the shunting yard algorithm, which is my favorite train-based algorithm in computer science.
Use shunting yard to convert your expression to Polish notation, then use the pseudo code to solve the equation. You can use lists as your "stack".
Keep in mind all your inputs are in strings, so make sure you convert them to integers when you are doing the actual math.