0

I am trying to get the margin of two items in an array, the array consists of 4 float values each, and I want the first array item to be subtracted by the first array item in the second array, and so on for the rest of the array.

My current code looks like this:

buyPrice = [
    '456.3',
    '2346.5',
    '123.5',
    '43.5',
]

sellPrice = [
    '426.3',
    '1346.5',
    '23.5',
    '13.5',
]

total = 0
amount = 0

userInput = int(input("How much coins do you want to spend?: "))

for x in buyPrice:
    while total <= userInput:
        total += float(x)
        amount += 1

    for y in sellPrice:
        k = float(x) - float(y)


    print("Profit: " + str(k))
    print("Total Cost: " + str("{:.1f}".format(total)))
    print("Buy: " + str(amount))
    print("----------")
    amount = 0
    total = 0
    k = 0

And the MARGIN output is this:

Item 1: -382.8
Item 2: -1303,0
Item 3: 20
Item 4: 30.0

Output should be:

Item 1: 30
Item 2: 1000
Item 3: 100
Item 4: 30

I think my other code is interrupting this, as I am trying to get: 1. Total amount of items I can buy with the user Input (product price: 10k - userInput = 100k - total amount of items that can be bought: 10 - and so on) 2. The profit the user can make if they are selling the item for a higher price. 3. The total cost of buying all the products (total = buyprice + buyprice)

I could just be completely of right now, it is early in the morning, but I am completely lost.

EDIT: After some debugging on this I see that "X" never changed value, it kept being "426.3", most likely because of my for loop? How can I fix that?

Thanks

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • Look into `zip`. Just iterate over `zip(buyPrice,sellPrice)` – John Coleman May 25 '20 at 11:29
  • I think you've got your buy price and sell price mixed up, you shouldn't buy higher than you sell – Nathan May 25 '20 at 11:32
  • yes, its supposed to be like that. You can buy from people (sellPrice), and you can sell to people (buyPrice), wont go into detail on how it works, but its correct haha –  May 25 '20 at 11:35

2 Answers2

0

The way I've understood the question:

You have 2 lists as input:

buyPrice = [
    '456.3',
    '2346.5',
    '123.5',
    '43.5',
]

sellPrice = [
    '426.3',
    '1346.5',
    '23.5',
    '13.5',
]

You want 1 list as output:

Item 1: 30
Item 2: 1000
Item 3: 100
Item 4: 30

You can do this in multiple ways. First, let's make the lists into lists of floats instead of strings:

buyPriceFloat = list(map(float, buyPrice))
sellPriceFloat = list(map(float, buyPrice))

Then you can subtract the second list from the first using:

difference = [sellPriceFloat[i] - buyPriceFloat[i] for i in range(len(sellPriceFloat))]

You can also use numpy arrays:

import numpy as np

buyPriceArray = np.array(buyPriceFloat)
sellPriceArray = np.array(sellPriceFloat)
difference = sellPriceArray - buyPriceArray
Nathan
  • 3,558
  • 1
  • 18
  • 38
  • yes, I want to substract the first value of the array (buyPrice), with the first value of sellPrice, meaning: 456.3 - 426.3 | then after that answer has been printed | 2346.5 - 1346.5 | and the rest. –  May 25 '20 at 11:34
  • It gave me the correct answer, though on multiple lines. You see I want it to display on the "product" its working on, there for the first price of each array is "product 1", and product 1 (when printing) should contain: margin, total cost, and amount to buy. My code in the first for loop does that, it adds the first buyPrice item on itself until it is either less than or equal to the userInput. Now in the terminal I am getting = margin: [30.0, 1000.0, 100.0, 30.0], it should have "30.0" on the first product, "1000.0" on the second product. –  May 25 '20 at 11:41
0

change your loop to this:

for x in range(len(buyPrice)):
    k = float(buyPrice[x])- float(sellPrice[x])

your problem is you have for loop inside a for loop which cause to every array item in buyprice to be substracted with every array item in sellprice. for example: buyprice[0] will be substracted with sellprice[0],sellprice[1],sellprice[2] and sellprice[4], and so on..

marksman123
  • 389
  • 2
  • 11
  • getting "TypeError: list indices must be integers or slices, not str" –  May 25 '20 at 11:36
  • @SimonSjöö it works for me, check again if your copied correctly. – marksman123 May 25 '20 at 11:40
  • @SimonSjöö `total = 0 amount = 0 userInput = int(input("How much coins do you want to spend?: ")) for x in range(len(buyPrice)): k = float(buyPrice[x])- float(sellPrice[x])` – marksman123 May 25 '20 at 11:40
  • oh no, now it works, weird. Thanks! –  May 25 '20 at 11:45
  • ah! Figured it out!, it doesnt work if I place "sellPrice" as the first argument in "k = float(buyPrice[x])- float(sellPrice[x])", so "k = float(sellPrice[y])- float(buyPrice[y])" does not work!, and if I do the code there you provided it shows a negative value! –  May 25 '20 at 11:46
  • @SimonSjöö you want it to get negative numbers or only positive ones? dont froget you cant indices with `y` because we erased that loop.. – marksman123 May 25 '20 at 11:50
  • I had to change it to y because I already had a for loop with "x", later realized it doesn't matter!! But yes, I want the numbers to be positive, not negative. –  May 25 '20 at 11:51
  • + I need it to write one by one, meaning I need the "30" to write on product "1", and "1000" to be on product "2", right now it prints all margin prices in each section, see in this pastebin: https://pastebin.com/fEG9aKje –  May 25 '20 at 11:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214579/discussion-between-marksman123-and-simon-sjoo). – marksman123 May 25 '20 at 11:53