无法将浮点值从python返回到HTML

2024-10-01 11:25:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图返回API上36种产品的价格,这些价格是浮动值。 下面是我试图从中获取值的产品:https://pastebin.com/4k1rif6h (如果您需要访问API,请检查下面的内容,这可能是必需的)

当我试图将浮点值返回到HTML时,我遇到了一个错误:TypeError:“float”对象不可编辑(我已经阅读了有关该问题的线程,但它对我没有帮助)

以下是我的python代码:

@app.route('/bresell')
def reSell():
    f = requests.get(
        'https://api.hypixel.net/skyblock/bazaar?key=[tell me if key will be needed for helping]').json()

    for x in npc_products:
        sellPriceNPC = f["products"][x]["sell_summary"][0]["pricePerUnit"]
    return render_template('resell.html', sellPriceNPC=sellPriceNPC)

下面是我在HTML上所做的工作:

<tbody>
      <tr>
        <td>temp</td>
        <td>temp</td>
        {% for sellFor in sellPriceNPC %}
        <td>{{ sellFor }}</td>
        {% endfor %}
      </tr>
    </tbody>

Tags: keyinhttpsapifor产品html价格
1条回答
网友
1楼 · 发布于 2024-10-01 11:25:34

您试图在sellPriceNPC(the for sellFor in sellPriceNPC)上迭代,但它不是一个iterable(它不是一个列表),因为您将每个浮点值直接分配给它:

sellPriceNPC = f["products"][x]["sell_summary"][0]["pricePerUnit"]

相反,您希望保留一个列表并附加到该列表:

sellPriceNPC = []

for x in npc_products:
    sellPriceNPC.append(f["products"][x]["sell_summary"][0]["pricePerUnit"])

return render_template('resell.html', sellPriceNPC=sellPriceNPC)

这样,您就可以正确地迭代列表并自行输出每个价格

相关问题 更多 >