I am trying to implement the sell function for PSET7. I wanted to check if the user owns the stock. My code below doesn't seem to work.

    @app.route("/sell", methods=["GET", "POST"])
    def sell():
    """Sell shares of stock."""

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

    #require user input stock symbol - create new template. Render apology if input is blank or stock does not exist
    if not request.form.get("symbol"):
        return apology("missing symbol")

    quoted = lookup(request.form.get("symbol"))

    if quoted == None:
        return apology("invalid symbol")

    #check user owns this stock
    rows = db.execute("SELECT symbol AS shareslist FROM portfolio WHERE user=:user", user = session["user_id"])
    shareslist = rows[0]["shareslist"]

    for row in shareslist:
        if quoted['symbol'] == row:
            return apology("you own this share {}".format(rows))
            return apology("you don't own this share {}".format(rows))

One problem is here shareslist = rows[0]["shareslist"]. It is only checking the first element of rows. What if the user owns 3 different stocks and the one you are searching for is in rows[2]?

Since you know what "symbol" to search for, consider adding that to the WHERE clause of the sql.

  • That makes a lot of sense. Thank you very much!! – karava Apr 26 '17 at 5:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .