I got syntax error at line def buy(): Can you help me find my mistake?

@app.route("/buy", methods=["GET", "POST"])


def buy():

"""Buy shares of stock"""

    if request.method == "POST":

        # Ensure symbol was submitted and valid
        if not request.form.get("symbol"):
            return apology("Missing symbol!", 400)

        stock = lookup(request.form.get("symbol"))

        if not stock:
            return apology("Invalid symbol!", 400)

        # Ensure shares were submitted and valid
        if not request.form.get("shares"):
            return apology("Missing shares!", 400)

        shares = int(request.form.get("shares"))

        if shares < 0:
            return apology("Shares must be positive integer!")

        # Ensure user has enough cash to buy stock
        cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])

        if not cash or float(cash[0]["cash"]) < stock["price"] * shares:
            return apology("Can't afford!", 400)

        price = float(stock["price"])
        total = shares * price
        updated_cash = float(cash[0]["cash"]) + total

        # Update cash
        db.execute("UPDATE users SET cash = :c WHERE id = :id", c=usd(updated_cash), id=session["user_id"])

        # Add stock to user's history
        db.execute("INSERT INTO history (symbol, shares, price, id) VALUES(:s, :sh, :p, :id)", s=stock["symbol"], sh=shares, p=price, id=session["user_id"])

        user_shares = db.execute("SELECT shares FROM portfolio where id = :id, symbol = :s", id=session["user_id"], s=stock["symbol"])

        if not user_shares:
            # Add new stock to user's portfolio
            db.execute("INSERT INTO portfolio (symbol, companyName, shares, price, total, id) VALUES(:s, :c, :sh, :p, :t, :id)", s=stock["symbol"], c=stock["name"], sh=shares, p=price, t=total, id=session["user_id"])
            # Update stock to user's portfolio
            share_total = user_shares[0]["shares"] + shares
            db.execute("UPDATE portfolio SET shares = :shares WHERE id = :id, symbol = :symbol", shares=share_total, id=session["user_id"], symbol=stock["symbol"])

        # Redirect user to home page
        return redirect("/")

        return render_template("buy.html")

"""Buy shares of stock""" needs to be indented

  • Hi there, thank you for your help. I tried as you said but somehow it still does not do the job. Do you have another way? – Anh Nhat Tran Dec 29 '18 at 4:05
  • @NhatAnh the IDE shows no syntax error if I indent that comment. Is it the IDE that is complaining? Or when you run flask run? – curiouskiwi Dec 29 '18 at 6:47
  • Either I indent that comment or not, it always shows syntax error when I run flask run and python application.py. – Anh Nhat Tran Dec 29 '18 at 14:28
  • When I copied that block of codes and pasted them in a new file, the IDE showed no syntax error. That's weird! – Anh Nhat Tran Dec 29 '18 at 14:58
  • Never mind. I found an error in my index() which is above buy(). When I fixed it, the IDE showed no syntax error. – Anh Nhat Tran Dec 31 '18 at 6:42

You must log in to answer this question.

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