I'm having an argument with check50 on pset7 finance, when it comes to the SELL part.
The web app works fine and I'm able to sell my stocks. When I run check50, though, it keeps telling me that the sell page doesn't have all the reuired elements (without specifying which one):
I double-checked my code and I double-checked the requirements and they match. The requirements are asking for a select element named "symbol" and an input text element named "shares". I have both as you can see here:
<form action="/sell" method="post">
<select class="form-control" id="select-sell" name="symbol">
<option disabled selected value>Selling</option>
{% for stock in stocks %}
<option>{{ stock.get('stock')|upper }}</option>
{% endfor %}
</select>
<div class="form-group">
<input autocomplete="off" class="form-control" name="shares" placeholder="Number of shares to sell" type="text"/>
</div>
<button class="btn btn-primary" type="submit">Sell</button>
</form>
While this is what's going on in application.py:
def sell():
"""Sell shares of stock"""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Ensure number shares was submitted
if (not request.form.get("shares")
or not request.form.get("shares").isdigit()
or float(request.form.get("shares")) < 0):
return apology("must specify a non negative number of shares to sell", 400)
# Get cumulative number of shares for selected stock
shares_sum = db.execute("SELECT SUM(shares) FROM :sid WHERE stock = :selected_stock",
sid=str(session["user_id"]),
selected_stock=request.form.get("symbol").lower())
# Check if user has enough shares of the stock he/she is trying to sell
if float(shares_sum[0].get('SUM(shares)')) < float(request.form.get("shares")):
return apology("cheeky...you cannot sell stocks that you don't have", 400)
# if he does and he's not selling all his/her shares
elif float(shares_sum[0].get('SUM(shares)')) >= float(request.form.get("shares")):
# Update the cash in the "users" table as well
update_cash = db.execute("UPDATE users SET cash = cash + :sell WHERE id = :sid",
sell = float(request.form.get("shares")) * lookup(request.form.get("symbol"))["price"],
sid=session["user_id"])
# While recording transaction (sale) to dedicated table in database
db.execute("INSERT INTO :user (stock, unitprice, shares, spent, cash, date, time)"
"VALUES (:symbol, :price, :number, :tot, :cash, :datestamp, :timestamp)",
user=str(session["user_id"]),
symbol=request.form.get("symbol").lower(),
price=float(lookup(request.form.get("symbol"))["price"]),
number=-float(request.form.get("shares")),
tot=-(float(lookup(request.form.get("symbol"))["price"])*float(request.form.get("shares"))),
cash=float(db.execute("SELECT cash FROM users WHERE id = :sid", sid=session["user_id"])[0].get("cash")),
datestamp=str(datetime.now(timezone('Australia/Sydney')).date()),
timestamp=str(datetime.now(timezone('Australia/Sydney')).time()))
# Redirect user to home page
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
else:
stocks = db.execute("SELECT DISTINCT stock FROM :sid ORDER BY stock", sid=str(session["user_id"]))
return render_template("sell.html", stocks = stocks)
I really have no idea what's wrong here (especially 'cause it's working!)
Any help is highly appreciated! Thanks!