I have problems with pset 9 finance. Since days I try to figure out what is wrong with my code for the sell route. I know that there a handfull of similar questions on StackExchange/StackOverflow but I just can't get my head around it. It stops executing at price = share["price"]. The rest of the app works fine, which uses similar concepts, so I must oversee something. Helpers.py uses the hard coded lookup() function provided by cs50 recently.

Here is the code

@app.route("/sell", methods=["GET", "POST"])
def sell():
    """Sell shares of stock"""
    id = session["user_id"]
    stocks = db.execute("SELECT * FROM stocks WHERE user_id = ?", id)

    if request.method == "GET":

        return render_template("sell.html", stocks=stocks)

        symbol = request.form.get("symbol")
        number_sell = int(request.form.get("number"))
        number_portfolio = db.execute("SELECT number_stocks FROM stocks WHERE symbol = ?", symbol)

        share = lookup(symbol)
        price = share["price"]
        name = share["name"]

        if number_sell > Number_portfolio:
            return apology("You picked more stocks than you have!", 403)

            total = price * number_sell
            number_stocks_after = number_portfolio - number_sell

        if number_stocks_after > 0:
            db.execute("UPDATE stocks SET number_stocks = ?", number_stocks_after)

            db.execute("DELETE FROM stocks WHERE name = ?", name)

            db.execute("INSERT INTO transaction (action, symbol, price, number, sum) VALUES (?,?,?,?,?)", "Sale", symbol, price, number_sell, total)
            db.execute("UPDATE users SET cash = cash + ?", total)

    return redirect("/")


{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}
<form action="/sell" method="post">
        <select class="form-select" aria-label="Default select example">
            <option selected>Open this select menu</option>
            {% for stock in stocks %}
            <option name="symbol" value ={{symbol}}>{{ stock.symbol }}</option>
            {% endfor %}
        <input type="text" id="number" name="number" min="1" max="100" step="1"></div>
    <button class="btn btn-primary" type="submit">Sell!</button>

{% endblock %}

Error message

File "/workspaces/78656287/scr9/finance/app.py", line 240, in sell
    price = share["price"]
TypeError: 'NoneType' object is not subscriptable

Any help appreciated, have nice weekend!

1 Answer 1


There are several problems in the HTML. One such is the select element needs the name attribute

to represent the name of the associated data point submitted to the server.

Here is some helpful doc

Use an HTML validator like Nu HTML Checker. Right-click the page and "View Source" (or whatever the browser calls it). Copy and paste into the checker text area. Any HTML errors can (likely will) cause a form to fail in unpredictable ways.

You must log in to answer this question.

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