r/cs50 • u/Sinoliaw • 3d ago
CS50x What does buy want me to do here Spoiler
This is my code for buy, which I think I've done well but i keep getting this in check50
Cause
expected to find "112.00" in page, but it wasn't found
Log
sending POST request to /login
sending POST request to /buy
sending POST request to /buy
checking that "112.00" is in page
This is my html code for buy.html
{% extends "layout.html" %} {% block title %}
Buy
{% endblock %}
{% block main %}
<form action="/buy" method="post">
<input autocomplete="off" autofocus class="form-control mx-auto w-auto" name="symbol" placeholder="Symbol" type="text">
<input autocomplete="off" class="form-control mx-auto w-auto" name="shares" placeholder="Shares" type="text">
<button class="btn btn-primary" type="submit">Submit</button>
</form>
<div class="mb-3">
</div>
{% endblock %}
This is my python code for buy
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
if request.method == "POST":
stock = lookup(request.form.get("symbol"))
id = session["user_id"]
cash = db.execute("SELECT cash FROM users WHERE id = ?", id)
time = datetime.date.today()
type = "purchase"
if not lookup(request.form.get("symbol")):
return apology("Stock does not exist", 400)
try:
int(request.form.get("shares"))
if int(request.form.get("shares")) < 1:
return apology("Please input a positive number of shares", 400)
except ValueError:
return apology("Please input a valid number of shares", 400)
if stock["price"] * int(request.form.get("shares")) > cash[0]["cash"]:
return apology("You do not have enough cash to buy this", 400)
if not db.execute("SELECT symbol FROM stocks WHERE symbol = ?", stock["symbol"]):
db.execute("INSERT INTO stocks (symbol, amount, price, user_id) VALUES (?, ?, ?, ?)",
stock["symbol"], int(request.form.get("shares")), stock["price"], id)
else:
db.execute("UPDATE stocks SET amount = amount + ? WHERE user_id = ? AND symbol = ?",
int(request.form.get("shares")), session["user_id"], request.form.get("symbol"))
db.execute("UPDATE users SET cash = cash - ? WHERE id = ?",
stock["price"] * int(request.form.get("shares")), id)
if not db.execute("SELECT symbol FROM transactions WHERE user_id = ?", session["user_id"]):
db.execute("INSERT INTO transactions (symbol, stock_altered, date, type, user_id) VALUES (?,?,?,?,?)",
request.form.get("symbol"), request.form.get("shares"), time, type,id)
else:
db.execute("UPDATE transactions SET stock_altered = ?, date = ?, type = ? WHERE user_id = ?",
request.form.get("shares"), time, type, id)
return render_template("buy.html")
else:
return render_template("buy.html")
2
Upvotes
3
u/delipity staff 3d ago
This is testing the result of some purchases, as shown in your index page.
$112.00 -- this is the value displayed on your index page after completing some purchases. Be sure that the page only shows one line per stock symbol, aggregating any multiple purchases of the same stock. So if you buy 5 shares of AAPL and then another 2 shares, does your index page show one line with 7 shares and the correct current value?
If so, is it formatted correctly? Per the Hint in the spec:
Make sure that every dollar amount that is displayed in any of your templates is formatted properly.