0

我正在尝试计算 3 件事,利润、总成本和金额。

用户投入他/她想要花费的硬币数量。每个产品都有一个买入价和一个卖出价,我需要得到利润、总成本和数量。

我在这里用这段代码来做这件事:

@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
        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)):
            amount.append(userInput * buy[x]) # This here calculates the amount user can buy

        total = []
        for x in range(len(buy)):
            total.append(amount[x] * buy[x])# This here calculates the total cost

        profit = []
        for x in range(len(buy)): 
            sell_profit = amount[x] * sell[x]
            buy_profit = amount[x] * sell[x]
            total_profit = sell_profit - buy_profit
            profit.append(total_profit) # This here calculates the profit user will make
        return render_template("flipper.html", userInput=userInput, product_name=product_name, profit=profit, amount=amount, total=total, sell=sell, buy=buy)
    else:
        return render_template("flipper.html", product_name=product_name)

问题是它返回错误的值,例如:

用户投入'123321'硬币。然后程序将执行:

123321 / buy = amount # how many of product x he/she can buy with 123321 coins.
amount * buy = total cost # the total cost
total cost(sell) - total cost(buy) # how much profit it will make

目前,它返回错误的值。123321 / 4.6 = 26808 (the amount) # 4.6是产品购买价格的示例

在浏览器中显示为:727594。这有点不对劲!我该如何解决?

4

1 回答 1

0

正如 roganjosh 所说,与烧瓶无关。答案只是调试程序,你可能会发现你的数学错误。也许打开一个测试文件,看看你的数学是否正确。

于 2020-05-29T15:29:04.763 回答