Python两次使用数组会导致错误的返回值

Python两次使用数组会导致错误的返回值,python,flask,Python,Flask,好的,所以我正在做一个项目,在那里有一个网站,可以计算出你可以从一个视频游戏的每个项目中获得多少利润 我在路上遇到了一些问题,但还是设法解决了其中的一些问题,我自己也在StackOverflow上得到了帮助。现在我所面临的问题,我似乎无法理解 我试图获得产品的“总成本”,我这样做(代码:1): 我从这里的代码中获取“金额”(代码:2): 现在的问题是,每当我试图获得总成本时,它几乎只是直接复制用户输入的任何内容,我猜这是因为- amount.append(userInput / sell[x])

好的,所以我正在做一个项目,在那里有一个网站,可以计算出你可以从一个视频游戏的每个项目中获得多少利润

我在路上遇到了一些问题,但还是设法解决了其中的一些问题,我自己也在StackOverflow上得到了帮助。现在我所面临的问题,我似乎无法理解

我试图获得产品的“总成本”,我这样做(代码:1):

我从这里的代码中获取“金额”(代码:2):

现在的问题是,每当我试图获得总成本时,它几乎只是直接复制用户输入的任何内容,我猜这是因为-

amount.append(userInput / sell[x])
当我试图从另一个for循环中达到那个“数量”时,它会导致问题吗?我可能完全错了,这就是为什么我需要你的帮助!:)

以下是页面的完整代码:

@app.route('/bflipper', methods=['POST', 'GET'])
def bFlipper():
    product_name = []
    f = requests.get(
        'https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2').json()
    for x in productNames:
        product_name.append(f["products"][x]["product_id"])
    if request.method == 'POST':
        userInput = request.form['coins']
        userInput = int(userInput)
        sell = [product['sell_summary'][0]['pricePerUnit']
                for product in f['products'].values() if product['sell_summary']]

        buy = [product['buy_summary'][0]['pricePerUnit']
               for product in f['products'].values() if product['buy_summary']]

        amount = []
        for x in range(len(buy)):
            totAmount = userInput / sell[x]
            amount.append(totAmount)

        total = []
        for x in range(len(buy)):
            total.append(amount[x] * sell[x])

        return render_template("flipper.html", userInput=userInput, product_name=product_name, amount=amount, total=total, sell=sell, buy=buy)
    else:
        return render_template("flipper.html", product_name=product_name)
以下是它在页面上的外观:

@app.route('/bflipper', methods=['POST', 'GET'])
def bFlipper():
    product_name = []
    f = requests.get(
        'https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2').json()
    for x in productNames:
        product_name.append(f["products"][x]["product_id"])
    if request.method == 'POST':
        userInput = request.form['coins']
        userInput = int(userInput)
        sell = [product['sell_summary'][0]['pricePerUnit']
                for product in f['products'].values() if product['sell_summary']]

        buy = [product['buy_summary'][0]['pricePerUnit']
               for product in f['products'].values() if product['buy_summary']]

        amount = []
        for x in range(len(buy)):
            totAmount = userInput / sell[x]
            amount.append(totAmount)

        total = []
        for x in range(len(buy)):
            total.append(amount[x] * sell[x])

        return render_template("flipper.html", userInput=userInput, product_name=product_name, amount=amount, total=total, sell=sell, buy=buy)
    else:
        return render_template("flipper.html", product_name=product_name)

我在“123321”中以“用户输入”的形式编写,您可以在“总成本”中看到,它几乎复制了我输入的内容,因此在“代码:1&代码:2”中的某个地方,它导致了问题

谢谢!
-Simon

total
最终成为
userInput
的副本,因为这正是您编程它要做的。浏览:在每个索引中,
数量[x]
是多少?那么,您将其设置为
userInput/sell[x]
。那么,每个
total[x]
将是什么<代码>总计[x]==金额[x]*卖出[x]==(用户输入/卖出[x])*卖出[x]==用户输入(大致上,在屏幕截图中可以看到一些浮点不精确)。因此,
total
中的所有条目最终都是
userInput
的副本


总之,我不确定“两次使用数组”是否一定是这里的问题。我不知道你的代码应该做什么,但这就是为什么它要这么做。

是的,我可能盯着这个标题看了一两分钟,不知道如何解释。而且,当你这样说的时候,我认为这是有道理的!我还试着做了两个单独的“金额”,你可能会明白,这并没有真正起作用。我是否可以通过不制作“total”和“amount”数组来绕过这个问题?而不是让它不断更新变量?我看到在flask中使用浮点数的“数组”帮助很大,因为我可以绕过这个问题:“TypeError:‘float’对象不可下标”。我在这个网站上工作了几天,仍然不知道如何绕过它。@SimonSjö在我看来,
total
amount
需要是数组,才能像那样显示在表中。是的,我让它工作了,只是在我的脑海里没有看到
total[x]==amount[x]*sell[x]==(userInput/sell[x])*sell[x]==userInput
,所以我真的很困惑为什么它会显示出来。无论如何,谢谢你的帮助!你能用英语解释一下计算背后的逻辑吗?我猜这是一个游戏,用户有一些余额,即123321,您通过产品,首先计算用户可以“购买”多少(即数量)(即使您通过
销售
)进行计算)。然后,我看不到表中计算
总成本的基本原理,即应如何计算?对不起,我对“销售”和“购买”也有点困惑。所以我在几分钟前看到,“卖出”与“买入”有某种联系,反之亦然。所以“卖出”意味着“买入”的价值。我在下面得到了帮助,我想我知道如何解决这个问题。当它被一个你需要了解一点的游戏包围时,很难解释这样的事情。我希望得到一个答案,当我在变量“userinput”中输入“123321”时,为什么表上显示“123321”,我得到了答案。
@app.route('/bflipper', methods=['POST', 'GET'])
def bFlipper():
    product_name = []
    f = requests.get(
        'https://api.hypixel.net/skyblock/bazaar?key=73ac0a44-4c41-4933-a9ee-b4095be2b6d2').json()
    for x in productNames:
        product_name.append(f["products"][x]["product_id"])
    if request.method == 'POST':
        userInput = request.form['coins']
        userInput = int(userInput)
        sell = [product['sell_summary'][0]['pricePerUnit']
                for product in f['products'].values() if product['sell_summary']]

        buy = [product['buy_summary'][0]['pricePerUnit']
               for product in f['products'].values() if product['buy_summary']]

        amount = []
        for x in range(len(buy)):
            totAmount = userInput / sell[x]
            amount.append(totAmount)

        total = []
        for x in range(len(buy)):
            total.append(amount[x] * sell[x])

        return render_template("flipper.html", userInput=userInput, product_name=product_name, amount=amount, total=total, sell=sell, buy=buy)
    else:
        return render_template("flipper.html", product_name=product_name)