简体   繁体   中英

Python using array twice results in wrong return value

Okay, so I am working on a project where there is a website, that can calculate how much profit you can make from each item from a video game!

I ran into a few issues down the road but managed to solve a few of them, both myself and got help here on StackOverflow. Now the issue I am at right now, I cant seem to figure out.

I am trying to get a "total cost" of products, which I do by doing this (code: 1):

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

I get 'amount' from this code right here(code: 2):

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

Now the issue is that whenever I try to get the total cost, it pretty much just straight up copies whatever the user have put in, and I am guessing it does so because of the -

amount.append(userInput / sell[x])

When I am trying to reach that "amount" from an different for loop, it causes issues? I might be completely wrong here, and that's why I need your help: :)

Here is the full code of the page:

@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)

Here is how it looks on the page:

图片

I wrote in '123321' as an 'userInput', and you can see on 'total cost' that it pretty much copied what I put in, so somewhere in 'code:1 & code:2' it is causing issues!

Thanks! - Simon

total ends up as copies of userInput because that's exactly what you programmed it to do. Walking through this: at each index, what is amount[x] ? Well, you set it equal to userInput / sell[x] . So, what will each of total[x] be? total[x] == amount[x] * sell[x] == (userInput / sell[x]) * sell[x] == userInput (roughly, with some floating point imprecision visible in your screenshot). So, all of the entries in total end up as copies of userInput like you observed.

In conclusion, I'm not sure that "using the array twice" is necessarily the issue here. I don't know what your code is supposed to do, but this is why it's doing what it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM