This calculator takes values that could be written in a browsers route path as a single string. It then returns the result as a number (or an error message).
Route paths use the '/' symbol so this can't be in our calculator. Instead we are using the '$' symbol as our divide operator.
You will be passed a string of any length containing numbers and operators:
* '+' = add        
* '-' = subtract  
* '*' = multiply 
* '$' = divide   
Your task is to break the string down and calculate the expression using this order of operations. (division => multiplication => subtraction => addition)
If you are given an invalid input (i.e. any character except `.0123456789+-*$`) you should return the error message:`'400: Bad request'`
### Remember:
1. Operations are infinite
2. Order of operations is imperitive
3. No `eval` or equivalent functions
**Examples:**
```python
calculate('1+1')     => '2'
calculate('10$2')    => '5'
calculate('1.5*3')   => '4.5'
calculate('5+5+5+5') => '20'
calculate('1000$2.5$5+5-5+6$6') =>'81'
calculate('10-9p')   =>  '400: Bad request'
```
### Further Notes - Parameters outside of this challenge:
- Brackets e.g. 10*(1+2)
- Square root, log, % or any other operators
- Unary minus (10*-1)
- Bad mathematical input (10**$10 or 5+5$)
- You may have to deal with floats
If enjoy this and want something harder please see http://www.codewars.com/kata/evaluate-mathematical-expression/ for making a much more complicated calculator. This is a good kata leading up to that.
def operation(op1, op2, op):
    if op == "*":
        return op1 * op2
    elif op == "+":
        return op1 + op2
    elif op == "-":
        return op1 - op2
    else:
        return op1 / op2

def calculate(expression):
    i, lst, num = 0, [], ""
    if "." in expression:
        if expression.replace(".", "").isdigit():
            return float(expression)
    while i < len(expression):
        if not expression[i].isdigit():
            if expression[i] in "+-*$":
                lst.append(int(num))
                lst.append(expression[i])
                num = ""
            else:
                return "400: Bad request"
        else:
            num += expression[i]
        i += 1
    if expression[i-1].isdigit():
        lst.append(int(num))
    length = len(lst)
    while(True):
        if "*" in lst and "$" in lst:
            index1, index2 = lst.index('*'), lst.index('$')
            if index1 < index2 and index1-1 > -1 and index1+1 < length and (type(lst[index1-1]) == int or type(lst[index1-1]) == float) and (type(lst[index1+1]) == int or type(lst[index1+1]) == float):
                lst[index1-1] = operation(lst.pop(index1-1), lst.pop(index1), lst[index1-1])
            elif index2-1 > -1 and index2+1 < length and (type(lst[index2-1]) == int or type(lst[index2-1]) == float) and (type(lst[index2+1]) == int or type(lst[index2+1]) == float):
                lst[index2-1] = operation(lst.pop(index2-1), lst.pop(index2), lst[index2-1])
            else:
                return "400: Bad request"
        elif "*" in lst:
            index = lst.index("*")
            if index-1 > -1 and index+1 < length and (type(lst[index-1]) == int or type(lst[index-1]) == float) and (type(lst[index+1]) == int or type(lst[index+1]) == float):
                lst[index-1] = operation(lst.pop(index-1), lst.pop(index), lst[index-1])
            else:
                return "400: Bad request"
        elif "$" in lst:
            index = lst.index("$")
            if index-1 > -1 and index+1 < length and (type(lst[index-1]) == int or type(lst[index-1]) == float) and (type(lst[index+1]) == int or type(lst[index+1]) == float):
                lst[index-1] = operation(lst.pop(index-1), lst.pop(index), lst[index-1])
            else:
                return "400: Bad request"
        elif "+" in lst and "-" in lst:
            index1, index2 = lst.index('+'), lst.index('-')
            if index1 < index2 and index1-1 > -1 and index1+1 < length and (type(lst[index1-1]) == int or type(lst[index1-1]) == float) and (type(lst[index1+1]) == int or type(lst[index1+1]) == float):
                lst[index1-1] = operation(lst.pop(index1-1), lst.pop(index1), lst[index1-1])
            elif index2-1 > -1 and index2+1 < length and (type(lst[index2-1]) == int or type(lst[index2-1]) == float) and (type(lst[index2+1]) == int or type(lst[index2+1]) == float):
                lst[index2-1] = operation(lst.pop(index2-1), lst.pop(index2), lst[index2-1])
            else:
                return "400: Bad request"
        elif "+" in lst:
            index = lst.index("+")
            if index-1 > -1 and index+1 < length and (type(lst[index-1]) == int or type(lst[index-1]) == float) and (type(lst[index+1]) == int or type(lst[index+1]) == float):
                lst[index-1] = operation(lst.pop(index-1), lst.pop(index), lst[index-1])
            else:
                return "400: Bad request"
        elif "-" in lst:
            index = lst.index("-")
            if index-1 > -1 and index+1 < length and (type(lst[index-1]) == int or type(lst[index-1]) == float) and (type(lst[index+1]) == int or type(lst[index+1]) == float):
                lst[index-1] = operation(lst.pop(index-1), lst.pop(index), lst[index-1])
            else:
                return "400: Bad request"
        else:
            break
    return lst.pop()
def operation(op1, op2, op):
    if op == "*":
        return op1 * op2
    elif op == "+":
        return op1 + op2
    elif op == "-":
        return op1 - op2
    else:
        return op1 / op2

def calculate(expression):
    i, lst, num = 0, [], ""
    if "." in expression:
        if expression.replace(".", "").isdigit():
            return float(expression)
    while i < len(expression):
        if not expression[i].isdigit():
            if expression[i] in "+-*$":
                lst.append(int(num))
                lst.append(expression[i])
                num = ""
            else:
                return "400: Bad request"
        else:
            num += expression[i]
        i += 1
    if expression[i-1].isdigit():
        lst.append(int(num))
    length = len(lst)
    while(True):
        if "*" in lst and "$" in lst:
            index1, index2 = lst.index('*'), lst.index('$')
            if index1 < index2 and index1-1 > -1 and index1+1 < length and (type(lst[index1-1]) == int or type(lst[index1-1]) == float) and (type(lst[index1+1]) == int or type(lst[index1+1]) == float):
                lst[index1-1] = operation(lst.pop(index1-1), lst.pop(index1), lst[index1-1])
            elif index2-1 > -1 and index2+1 < length and (type(lst[index2-1]) == int or type(lst[index2-1]) == float) and (type(lst[index2+1]) == int or type(lst[index2+1]) == float):
                lst[index2-1] = operation(lst.pop(index2-1), lst.pop(index2), lst[index2-1])
            else:
                return "400: Bad request"
        elif "*" in lst:
            index = lst.index("*")
            if index-1 > -1 and index+1 < length and (type(lst[index-1]) == int or type(lst[index-1]) == float) and (type(lst[index+1]) == int or type(lst[index+1]) == float):
                lst[index-1] = operation(lst.pop(index-1), lst.pop(index), lst[index-1])
            else:
                return "400: Bad request"
        elif "$" in lst:
            index = lst.index("$")
            if index-1 > -1 and index+1 < length and (type(lst[index-1]) == int or type(lst[index-1]) == float) and (type(lst[index+1]) == int or type(lst[index+1]) == float):
                lst[index-1] = operation(lst.pop(index-1), lst.pop(index), lst[index-1])
            else:
                return "400: Bad request"
        elif "+" in lst and "-" in lst:
            index1, index2 = lst.index('+'), lst.index('-')
            if index1 < index2 and index1-1 > -1 and index1+1 < length and (type(lst[index1-1]) == int or type(lst[index1-1]) == float) and (type(lst[index1+1]) == int or type(lst[index1+1]) == float):
                lst[index1-1] = operation(lst.pop(index1-1), lst.pop(index1), lst[index1-1])
            elif index2-1 > -1 and index2+1 < length and (type(lst[index2-1]) == int or type(lst[index2-1]) == float) and (type(lst[index2+1]) == int or type(lst[index2+1]) == float):
                lst[index2-1] = operation(lst.pop(index2-1), lst.pop(index2), lst[index2-1])
            else:
                return "400: Bad request"
        elif "+" in lst:
            index = lst.index("+")
            if index-1 > -1 and index+1 < length and (type(lst[index-1]) == int or type(lst[index-1]) == float) and (type(lst[index+1]) == int or type(lst[index+1]) == float):
                lst[index-1] = operation(lst.pop(index-1), lst.pop(index), lst[index-1])
            else:
                return "400: Bad request"
        elif "-" in lst:
            index = lst.index("-")
            if index-1 > -1 and index+1 < length and (type(lst[index-1]) == int or type(lst[index-1]) == float) and (type(lst[index+1]) == int or type(lst[index+1]) == float):
                lst[index-1] = operation(lst.pop(index-1), lst.pop(index), lst[index-1])
            else:
                return "400: Bad request"
        else:
            break
    return lst.pop()