I've ran into an issue while running Check50 on what I thought was my functioning solution for pset7, namely it doesn't find the correct value in the index after trying to run the buy function.
This is the error message I'm currently getting:
:( buy handles valid purchase
expected to find "112.00" in page, but it wasn't found
Log
sending POST request to /login
sending POST request to /buy
checking that "112.00" is in page
I've checked the formatting of the values on the index page (using usd()
) and have been messing around with the data types on the buy function to no avail. To my eyes the website runs as it should, so feels like I'm debugging blindly here.
One issue I've noticed all day though is that the lookup()
function runs pretty slowly and fairly often fails to return any values, causing the index page to fail to load. Not sure if this is due to the Yahoo Finance servers being pressured atm...
Also attaching my code for the buy function below:
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "POST":
# Check if stock and amount have been inserted
if not request.form.get("symbol"):
return apology("please provide symbol", 400)
elif not request.form.get("shares"):
return apology("please provide amount", 400)
else:
# Store amount and information about stock
try:
amount = int(request.form.get("shares"))
if amount < 0:
return apology("please input positive integer", 400)
except:
return apology("please input positive integer", 400)
stockinfo = lookup(request.form.get("symbol"))
# Exits if stock is invalid
if stockinfo == None:
return apology("stock not found", 400)
# Retrieve price and current cash holdings
price = float(amount) * stockinfo['price']
currentcash = db.execute(
"SELECT cash FROM users WHERE id = :sessionid", sessionid=session["user_id"])
# Check if user has enough cash
if float(currentcash[0]['cash']) < price:
return apology("not enough cash", 400)
else:
# Update user cash data & create new entry in history db
db.execute("UPDATE users SET cash = :newsum WHERE id = :sessionid",
newsum=currentcash[0]['cash'] - price, sessionid=session["user_id"])
insert = db.execute("INSERT INTO history (id, amount, stock, price) VALUES(:sessionid, :amount, :stock, :price)",
sessionid=session["user_id"], amount=amount, stock=stockinfo['name'], price=stockinfo['price'])
return redirect("/")
else:
return render_template("buy.html")
If anyone can spot any issue I would be over the moon, thanks in advance!