我正在嘗試“執(zhí)行”股票的(假)買單。cash = cashcheck["cash"]我在該行遇到錯(cuò)誤。它說(shuō)索引必須是整數(shù),但我們談?wù)摰氖清X,所以它不能是一個(gè)整數(shù),因?yàn)樯婕靶?shù)......我認(rèn)為。非常感謝您的幫助!代碼:@app.route("/buy", methods=["GET", "POST"])@login_requireddef 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)
1 回答

胡說(shuō)叔叔
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
cashcheck
是與查詢匹配的行的列表。
如果您只希望匹配一行,請(qǐng)使用cashcheck[0]["cash"]
.
添加回答
舉報(bào)
0/150
提交
取消