亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

類型錯誤:列表索引必須是整數 - 處理金錢

類型錯誤:列表索引必須是整數 - 處理金錢

DIEA 2023-02-07 10:41:26
我正在嘗試“執行”股票的(假)買單。cash = cashcheck["cash"]我在該行遇到錯誤。它說索引必須是整數,但我們談論的是錢,所以它不能是一個整數,因為涉及小數......我認為。非常感謝您的幫助!代碼:@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 回答

?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

cashcheck是與查詢匹配的行的列表。

如果您只希望匹配一行,請使用cashcheck[0]["cash"].


查看完整回答
反對 回復 2023-02-07
  • 1 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號