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"])
@login_required
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)
else:
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)
else:
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)
else:
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("/")
HTML
{% extends "layout.html" %}
{% block title %}
Hello!
{% 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 %}
</select>
<input type="text" id="number" name="number" min="1" max="100" step="1"></div>
<button class="btn btn-primary" type="submit">Sell!</button>
</form>
{% 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!