0

i have tried so many options but to no avail pls below are the code i wrote.`@app.route("/buy", methods=["GET", "POST"]) @login_required def buy(): """Buy shares of stock""" if request.method == "POST": symbol = request.form.get("symbol").upper() item = lookup(symbol)

    if not symbol:
        return apology("MISSING SYMBOL")

    elif not item:
        return apology("MISSING SYMBOL")
    # Check share is numeric data type
    try:
        shares = int(request.form.get("shares"))
    except:
        return apology("INVALID SHARES")
    # Check shares is positive number
    if not (shares >= 0):
        return apology("INVALID SHARES")

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

    item_name = item["name"]
    item_price = item["price"]
    total_price = item_price * shares

if cash < total_price:
    return apology("not enough cash")
    db.execute("UPDATE users SET cash = ? WHERE id = ?",cash - total_prices, user_id)
    db.execute("UPDATE users SET cash = ? WHERE id = ?",cash - total_prices, user_id)
    db.execute("INSERT INTO transactions(user_id, name, symbol, shares, price, type) VALUES(?, ?, ?, ?, ?, ?,)",user_id, item_name, shares, item_price, 'buy',symbol)
    return redirect('/')
else:
    return render_template("buy.html")`

1 Answer 1

1

Assuming the transactions table is the portfolio and assuming the program indenting is as shown:

if cash < total_price:
    return apology("not enough cash")
    db.execute("UPDATE users SET cash = ? WHERE id = ?",cash - total_prices, user_id)
    db.execute("UPDATE users SET cash = ? WHERE id = ?",cash - total_prices, user_id)
    db.execute("INSERT INTO transactions(user_id, name, symbol, shares, price, type) VALUES(?, ?, ?, ?, ?, ?,)",user_id, item_name, shares, item_price, 'buy',symbol)
    return redirect('/')

Nothing after return apology("not enough cash") executes because program has returned from the buy function.

3
  • now i see thanks so much i really appreciate
    – amaka
    Feb 14 at 10:01
  • pls how do you expect me to restructure my code
    – amaka
    Feb 14 at 12:31
  • how do i restructure my code to work pls
    – amaka
    Feb 14 at 12:55

You must log in to answer this question.

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