I'm puzzled why check50
shows me this error despite my portfolio and SQLite database correctly reflecting the sold shares
:( sell handles valid sale expected to find "56.00" in page, but it wasn't found
Following this answer, I tried running the staff solution
Below are the outputs after I bought 10 shares of BSE and sold 5 of them in both solutions:
Here is my "sell" method code in application.py
:
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
user_id = session["user_id"]
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
rows = db.execute("SELECT stock, SUM(shares) FROM history WHERE id = :id GROUP BY stock", id=user_id)
selected_sym = request.form.get("symbol")
try:
shares = int(request.form.get("shares"))
if shares <= 0:
return apology("Please enter valid shares", 400)
for i in range(len(rows)):
if rows[i]["stock"] == selected_sym:
if shares > rows[i]["SUM(shares)"]:
return apology("Please enter valid shares", 400)
except ValueError:
return apology("Please enter valid shares", 400)
quote = lookup(selected_sym)
if quote != None:
time = datetime.now()
current_time = time.strftime('%Y-%m-%d %H:%M:%S')
rows = db.execute("SELECT cash FROM users WHERE id = :id", id=user_id)
updated_cash = round(rows[0]["cash"] + (shares * quote["price"]), 2)
shares = -shares
update_users = db.execute("UPDATE users SET cash = :cash WHERE id = :id", id=user_id, cash=updated_cash)
insert_history = db.execute("INSERT INTO history (id, method, stock, price, shares, time) VALUES(:id, :method, :stock, :price, :shares, :time)",
id=user_id, method="SOLD", stock=quote["symbol"], price=quote["price"], shares=shares, time=current_time)
if not update_users:
return apology("Sorry, stock could not be bought")
if not insert_history:
return apology("Sorry, stock bought could not be saved")
return redirect(url_for("index"))
return apology("Sorry, this stock is not valid!")
# User reached route via GET (as by clicking a link or via redirect)
else:
rows = db.execute("SELECT DISTINCT stock FROM history WHERE id = :id", id=user_id)
return render_template("sell.html", stocks=rows)
return apology("Something went wrong")
I do not think there is a problem with formatting here as I have passed the following check50
tests:
:) buy handles valid purchase
Log
sending POST request to /login
sending POST request to /buy checking that "112.00" is in page
checking that "9,888.00" is in page
Complete Log: https://cs50.me/checks/bf480fb67ba5c92d487fb4f201f3bd155e4512d7
Please help me understand what is the actual problem here (is it a check50 bug or am I doing something wrong here?), as I am really flustered for not being able to pass the last check50 test despite evidently having implemented it correctly.
I'll be happy to share my IDE workspace if need be.
Edit:
As suggested in an answer, I added this to the top of my lookup
function in helpers.py
if symbol == "AAAA":
return {"name": "Test A", "price": 28.00, "symbol": "AAAA"}
Bought 4 shares of AAAA and then sold 2 shares of it and got an output shown by this screenshot:
lookup
? This check50 failure is invariably a problem in index (route or .html).usd
filter for display? I failed at some point because I did the formatting myself at one point and displayed like$123.4
instead of$123.40
. Changing all tousd
fixed that for me.$123.4
instead of$123.40
) and that is whycheck50
passes the:) buy handles valid purchase
test. It is only in the sell portion that I am getting an error. Please check this log: cs50.me/checks/bf480fb67ba5c92d487fb4f201f3bd155e4512d7 and see if you can help me