I am working on sell and I am getting an error: "TypeError: 'NoneType' object is not subscriptable" on the variable "price". I am not sure why as this is more or less the exact same code I have in buy which is working fine (thus, I don't think it is a problem with the lookup function). I do have "symbol" in sell.html so I think it is finding a symbol but I'm guessing it has to be related to the way I've implemented the drop down. It doesn't seem to have any problem pulling the number of shares from the form. Any guidance on where to look would be appreciated, I can't really do anything to move forward on sell until I figure this out and I just started.
application.py - sell function
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Make sure user chose a stock from the dropdown menu.
if not request.form.get("symbol"):
# return apology (relevant error message, e.g., "Missing username!")
return apology("please choose a stock from the drop down menu", 403)
# Ensure user specified number of shares:
elif not request.form.get("shares"):
# return apology (relevant error message, e.g., "Missing username!")
return apology("please enter the number of shares you would like to sell", 403)
else:
# Look up current stock price.
sellquote = lookup(request.form.get("symbol"))
# Placeholder for how much cash the user currently has.
usercash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
# Pull out the aggregate number of shares that the user owns by stock symbol.
holdings = db.execute("SELECT SUM(shares) as sumshares FROM transactions WHERE userid = :userid GROUP BY symbol", userid = session["user_id"])
# Define variables
sharestosell = float(request.form.get("shares"))
price = sellquote["price"]
sharesowned = holdings[0]['sumshares']
transactioncost = sharestosell * price
cashleft = float(usercash[0]['cash'] + transactioncost)
# Once we know which stock the user wants to sell and how many shares we need to know if they actually have that many to sell.
# If you have enough shares of a given stock (aggregated) to sell...
if sharestosell >= sharesowned:
#then sell that stock
selltransaction = db.execute("INSERT INTO transactions (userid, symbol, price, shares, timestamp) VALUES(:userid, :symbol, :price, :shares, datetime('now'))",
userid=session["user_id"], symbol=sellquote["symbol"], price=price, shares=sharestosell)
# Update the cash to reflect the purchase they just made.
updatecash = db.execute("UPDATE users SET cash = :cashleft WHERE id = :id", cashleft=cashleft, id=session["user_id"])
return redirect("/")
# If cash is less than the total value of the attempted purchase
else:
# Tell user they don't have enough shares
return apology("enter a number of shares less than or equal to the number you own", 403)
# User reached route via GET (as by clicking hhhha link or via redirect)
else:
sales = db.execute("SELECT symbol FROM transactions WHERE userid = :userid GROUP BY symbol ORDER BY symbol ASC", userid = session["user_id"])
for sale in sales:
symbol = sale["symbol"]
return render_template("sell.html", stocks=sales)
sell.html
{% extends "layout.html" %}
{% block title %}
Sell Stock
{% endblock %}
{% block main %}
<form action="/sell" method="post">
<div class="form-group">
<select autocomplete="off" autofocus class="form-control" name="symbol" placeholder="e.g., AMZN" type="text"/>
{% for stock in stocks %}
<option value = "Symbol">{{ stock.symbol }}</option></option>
{% endfor %}
</select>
</div>
<div class="form-group">
<input class="form-control" name="shares" placeholder="Number of Shares (e.g., 50)" type="number"/>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
{% endblock %}