烧瓶进行计算会返回错误的值

时间:2020-05-29 14:03:22

标签: python math

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

用户放入他/她想要花费的硬币数量。每个产品都有买价和卖价,我需要获得利润,利润,总成本和金额。

我在这里用这段代码来做到这一点:

@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。哪有错!我该如何解决?

1 个答案:

答案 0 :(得分:0)

正如罗根乔什(roganjosh)所说,这与长颈瓶无关。答案仅仅是调试程序,您可能会发现数学错误。也许打开一个测试文件,看看那里的数学是否正确。