pset7 / finance.py错误:'TypeError:不支持的格式字符串传递给list .__ format__'

时间:2018-12-15 17:47:39

标签: python flask flask-sqlalchemy

我仍然遇到相同的错误“购买处理有效的购买应用程序引发了异常(有关详细信息,请参阅日志)”,而且我不知道我应该在代码中进行哪些修改以传递错误。

(“ Aandeel”是荷兰人的股票)。

这是我的代码:

def buy():
"""Buy shares of stock"""
# geldige invoer
if request.method == "POST":
    # check voor geldige invoer
    aandeel = lookup(request.form.get("symbol"))
    if not aandeel:
        return apology("Invalid input, please try again", 400)
    # check voor geldige invoer
    try:
        shares = int(request.form.get("shares"))
        if shares < 0:
            return apology("Shares must be a positive integer", 400)
    except:
        return apology("Invalid input, please try again", 400)
    if shares == None:
        return apology("Invalid input, please try again", 400)
    # hoeveelheid geld van user
    geld = usd(db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"]))
    current_cash = usd(geld[0]["cash"])
    # ga na of het voldoende geld is
    if current_cash < usd(shares * aandeel["price"]):
        return apology("Unfortunately, you don't have enough money", 400)
    else:
        # voer transactie in
        db.execute("INSERT INTO transactions (id, symbol, shares, price, total, date_time) VALUES (:id, :symbol, :shares, :price, :total, DATETIME())",
                   id=session["user_id"], symbol=aandeel["symbol"], shares=shares, price=usd(aandeel["price"]), total=usd(aandeel["price"] * shares))
        # update de cash van de user
        db.execute("UPDATE users SET cash = cash - :purchase WHERE id:id;", \
                   purchase=usd(aandeel["price"] * shares), id=session["user_id"])
        # Select user shares of that symbol
        user_shares = db.execute("SELECT shares FROM portfolio \
                       WHERE id=:id AND symbol=:symbol", \
                       id=session["user_id"], symbol=aandeel["symbol"])
        # ingeval aankoop aandeel dat user nog niet bezit
        if not user_shares:
            db.execute("INSERT INTO portfolio (id, symbol, shares, price, total) \
                    VALUES(id:, :symbol, :shares, :price, :total)", \
                    symbol=aandeel["symbol"], shares=shares, price=usd(aandeel["price"]), \
                    total=usd(shares * aandeel["price"]), id=session["user_id"])
        # ingeval user al aandelen van dat bedrijf bezit
        else:
            shares_total = (user_shares[0]["shares"] + shares)
            db.execute("UPDATE portfolio SET shares=:shares \
                    WHERE id=:id AND symbol=:symbol", \
                    shares=shares_total, id=session["user_id"], \
                    symbol=aandeel["symbol"])
    return redirect("/", 400)
else:
    return render_template("buy.html")

跟踪:

enter image description here

1 个答案:

答案 0 :(得分:0)

问题似乎出在此调用站点的usd函数中:

geld = usd(db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"]))

回溯显示错误消息

TypeError: unsupported format string passed to list.__format__

该行

return f'${value:,.2f}'

错误消息对应于value语句中的return,它是一个列表,而不是预期的数字:

>>> value = 10  
>>> f'${value:,.2f}'
'$10.00'

>>> value = [10]
>>> f'${value:,.2f}'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to list.__format__

因此,似乎帮助程序期望使用单个值,例如10,但是db.execute返回的是列表,例如[10]。

因此解决方法是将db.execute(...)[0]传递给usd

请注意,您可能需要更改或删除下一行:

current_cash = usd(geld[0]["cash"])