I am working on pset9 Finance. I have implemented the functionality for purchasing stocks, and it works well when I manually test it. However, when I run the CS50's check50 validation, it returns an error: "exception raised in application: TypeError: 'NoneType' object is not subscriptable" on the ":( buy handles valid purchase" validation.
I have checked my code thoroughly and cannot identify the source of this issue. The error message suggests that I am trying to access an element of a NoneType object using subscript notation, but I cannot reproduce the error when I manually test the functionality.
I have reviewed my code and ensured that I handle potential None values properly. I also checked the data types and made sure that the necessary data is provided before accessing it.
I am seeking guidance on how to troubleshoot this issue further and understand why it fails during the check50 validation but not during manual testing. Any insights or suggestions on what I may be overlooking would be greatly appreciated.
Also, this question was written by ChatGPT, because it is after midnight and I have been trying to figure it out for hours now.
Here is my python/flask code
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "GET":
return render_template("purchase.html")
elif request.method == "POST":
# Check for missing values
if not request.form.get("symbol"):
return apology("Enter stock symbol i.e. NFLX", 400)
elif not request.form.get("shares"):
return apology("Enter amount of stock", 400)
# Check for numbers
if request.form.get("shares").isdecimal() == False:
return apology("Enter a number", 400)
# Check for decimal
if "," in request.form.get("shares") or "." in request.form.get("shares"):
return apology("Cannot buy fractional", 400)
# Get stock dictionary
quote = request.form.get("symbol")
amount = int(request.form.get("shares"))
# Check for invalid symbol
if lookup(quote) == None:
return apology("Enter stock symbol i.e. NFLX", 400)
stock = lookup(quote)
# Check for negative number
if amount <= 0:
return apology("Enter positive number", 400)
# Check for funds
price = (float(stock["price"]) * float(amount))
funds = db.execute("SELECT cash FROM users WHERE id = ?", session["user_id"])
funds = float(funds[0]["cash"])
if funds < price:
return apology("insufficient funds", 400)
date = datetime.now()
# Add purchase into database
db.execute("INSERT INTO stocks (stock, amount, date_time, buyer_id) VALUES (?, ?, ?, ?);", stock["name"], amount, date, session["user_id"])
# Add purchase into transaction
db.execute("INSERT INTO transactions (stock, amount, type, date_time, buyer_id) VALUES (?, ?, ?, ?, ?);", stock["name"], amount, 'buy', date, session["user_id"])
# Reduce funds
funds = funds - price
db.execute("UPDATE users SET cash = ? WHERE id = ?", funds, session["user_id"])
return redirect("/")
else:
return apology("Unknown error") # Just in case
And here is my HTML code:
{% extends "layout.html" %}
{% block title %}
Buy
{% endblock %}
{% block main %}
<form action="/buy" method="post">
<div class="mb-3">
<input autocomplete="on" autofocus class="form-control mx-auto w-auto" id="symbol" name="symbol" placeholder="Stock symbol" type="text" required>
</div>
<div class="mb-3">
<input autocomplete="on" autofocus class="form-control mx-auto w-auto" id="shares" name="shares" placeholder="Stock amount" type="number" min="1" required>
</div>
<button class="btn btn-primary" type="submit">Buy stock</button>
</form>
{% endblock %}
Can you give me a hand, please? Thanks very much!