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 

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"])
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)

            # Store amount and information about stock
                amount = int(request.form.get("shares"))
                if amount < 0:
                    return apology("please input positive integer", 400)
                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)

                # 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("/")
        return render_template("buy.html")

If anyone can spot any issue I would be over the moon, thanks in advance!

  • I am having the same problem right now. I feel the code works as it should and that there could be a formatting issue. Did u solve this issue?
    – Sashi
    Feb 11, 2018 at 12:08
  • Nope, haven't hade any more luck with it...
    – mswsn
    Feb 21, 2018 at 9:01

1 Answer 1


It looks like you aren't actually using the symbol in your database?

Also, do you not have a holdings table? I don't see that being updated. Does your index.html use the history table to calculate the holdings? How does it know that it's a purchase and not a sale?

  • Did some reading and apparently the latest lookup function returns the symbol when accessing both ['symbol'] and ['name'], didn't know that when I wrote the code though. And yes, right now I only use a history table and the GROUP BY and SUM functions to determine user's current shares of a stock.
    – mswsn
    Feb 7, 2018 at 9:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .