Reputation: 3
A problem appears to me and I do not know where it came from. I converted everything to float and it did not work. I tried to search for a solution but I could not find it. It is only in this function.
:( buy handles valid purchase expected to find "112.00" in page, but it wasn't found
my code if can any one see it and tell me how fixed
code{
def buy():
if request.method == "POST":
symbol = request.form.get("symbol").upper()
shares = request.form.get("shares")
if not symbol:
return apology("must provide symbol", 400)
elif not shares or not shares.isdigit() or int(shares) <= 0:
return apology("must provide a positive integer number of shares", 400)
quote = lookup(symbol)
if quote is None:
return apology("symbol not found", 400)
price = quote["price"]
total_cost = int(shares) * price
cash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])[0]["cash"]
if cash < total_cost:
return apology("not enough cash", 400)
# Update users table
db.execute("UPDATE users SET cash = cash - :total_cost WHERE id = :user_id",
total_cost=total_cost, user_id=session["user_id"])
# Add the purchase to the history table
db.execute("INSERT INTO transactions (user_id, symbol, shares, price) VALUES (:user_id, :symbol, :shares, :price)",
user_id=session["user_id"], symbol=symbol, shares=shares, price=price)
flash(f"Bought {shares} shares of {symbol} for {usd(total_cost)}!")
return redirect("/")
else:
return render_template("buy.html")
}
i dont know i try check the code but still not work
Upvotes: 0
Views: 1141
Reputation: 1
What I have found, after fighting for some weeks, is that the error comes because it has not been considered the case when one already has bought shares from a company. So, in the code must be considered that shares should be added and the transactions table must be updated instead of inserting a new raw.
Upvotes: 0
Reputation: 3
in index.html just make like he want display total value and his my code
Upvotes: 0