Almost finished Pset7 but I'm getting an error on Check50 that I don't understand. The log file says:

    :( buy handles valid purchase

    Log 
    sending POST request to /login 
    sending POST request to /buy 
    exception raised in application: TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' 

Here's my code for the buy application:

    """Buy shares of stock"""

    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # Ensure stock symbol was submitted
        if not request.form.get("symbol"):
            return apology("Missing stock's symbol", 400)

        else:
            # Ensure float number of shares was submitted
            try:
                shares = float(request.form.get("shares"))

            except ValueError:
                return apology("Please submit number of shares", 400)

            # Ensure positive integer number of shares was submitted
            if (shares % 1) != 0 or (shares < 0):
                return apology("Please submit number of shares", 400)

            # Check if stock exists
            elif (lookup(request.form.get("symbol")) == None):
                return apology("Stock does not exist", 400)

            else:
                # Check stock and store info in variables
                stock = lookup(request.form.get("symbol"))
                stockname = stock['name']
                stockprice = stock['price']
                stocksymbol = stock['symbol']
                totalvalue = (stockprice * shares)

                # Calculate current date and time
                now = datetime.now()
                d = now.date()
                t = now.time()

                # Lookup user's available cash and store in variable
                user_cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id = session["user_id"])

                # Ensure user has enough cash
                if totalvalue > user_cash[0]['cash']:
                    return apology("Not enough cash to buy", 403)

                else:
                    # add transaction to the portfolio table
                    result = db.execute("INSERT INTO portfolio (user_id, stock, symbol, price, numshares, valuestocks, date, time, buyorsell) VALUES(:user_id, :stock, :symbol, :price, :numshares, :valuestocks, :date, :time, :buyorsell)", user_id = session["user_id"], stock = stockname, symbol = stocksymbol, price = stockprice, numshares = shares, valuestocks = totalvalue, date = d, time = t, buyorsell = 'BUY')

                    if result != None:
                        db.execute("UPDATE users SET cash = :cash WHERE id = :user_id", cash = user_cash[0]['cash'] - totalvalue, user_id = session["user_id"])

                        # Redirect user to index
                        flash('BUY SUCCESSFUL')
                        return redirect("/")

    # User reached route via GET (as by clicking a link or via redirect)
    else:
        return render_template("buy.html")


Looking at the log file, there is a problem with the application trying to multiply two variables, one of which is of type 'NonType' and the other is an int. Since there is only one line of code where I multiply two values, it should result from:

    totalvalue = (stockprice * shares)
But given all the other earlier IF conditions I cannot see how either `stockprice` or `shares` can have the value `None` and so is of type `NonType`.

Also the buy operation works as expected, when I type in an existing stock, a valid number of shares and have enough cash. Can someone help me out with a suggestion or knows where this error might be coming from?