@app.route("/buy", methods=["GET", "POST"]) @login_required def buy(): """Buy shares of stock""" if request.method == "GET": return render_template("buy.html") else: symbol = request.form.get("symbol") stock = lookup(symbol.upper())

    if not symbol:
        return apology("Must give symbol")
    elif not stock:
        return apology("Invalid symbol")
        shares = int(request.form.get("shares"))
        return apology("Can't purchase partial shares, must be an integer")

    if shares <= 0:
        return apology("Share not allowed")

    user_id = session["user_id"]
    user_cash_db = db.execute("SELECT cash FROM users WHERE id = ?", user_id)
    user_cash = usd(user_cash_db[0]["cash"])

    transaction_value = shares * stock["price"]

    if user_cash < transaction_value:
        return apology("Not Enough Cash")
        updt_cash = user_cash - transaction_value
        db.execute("UPDATE users SET cash = ? WHERE id = ?", updt_cash, user_id)

        date = datetime.datetime.now()
        db.execute("INSERT INTO transactions(user_id, symbol, shares, price, date) VALUES(?,?,?,?,?)",
                    user_id, stock["symbol"], shares, stock["price"], date)

        flash("Bought a Share!")

    return redirect("/")

1 Answer 1


usd function returns a string so this user_cash = usd(user_cash_db[0]["cash"]) is a problem.

You must log in to answer this question.

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