我正在尝试“执行”股票的(假)买单。cash = cashcheck["cash"]我在该行遇到错误。它说索引必须是整数,但我们谈论的是钱,所以它不能是一个整数,因为涉及小数......我认为。
非常感谢您的帮助!
代码:
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "GET":
return render_template("/buy.html")
else:
# collect user input - symbol
symbol = request.form.get("symbol").upper()
# if input is blank or symbol doesn't exist, return apology
if not symbol:
return apology("You must enter a stock symbol.", 300)
# collect user input - # of shares
shares = int(request.form.get("shares"))
# if blank or not a positive integer, return apology
if not shares:
return apology("Enter a valid number of shares.", 300)
# pull current price info from API
quote = lookup(symbol)
shareprice = quote["price"]
totalprice = shareprice * shares
# check users table to see how much cash user has
cashcheck = db.execute("SELECT cash FROM users WHERE id = :userid", userid = session["user_id"])
cash = cashcheck["cash"]
if cash >= totalprice:
# in transactions table, insert userID, symbol, shares, shareprice, and totalprice
# transID should be autogenerated and autoincremented. date is also autofilled by SQLite.
db.execute("INSERT INTO transactions (userID, symbol, shares, shareprice, totalprice) VALUES (:userid, :symbol, :shares, :shareprice, :totalprice)",
userid=session["user_id"], symbol=symbol, shares=shares, shareprice=shareprice, totalprice=totalprice)
cash = cash - totalprice
# update cash balance
db.execute("UPDATE users SET cash = :cash WHERE id = :userid", userid = session["user_id"])
#return index
return ("/")
else:
# else, return apology (not enough cash)
return apology("Not enough cash balance to make execute this order.", 300)
胡说叔叔
相关分类