为了账号安全,请及时绑定邮箱和手机立即绑定

类型错误:列表索引必须是整数 - 处理金钱

类型错误:列表索引必须是整数 - 处理金钱

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 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信