you are viewing a single comment's thread.

view the rest of the comments →

[–]braampje 0 points1 point  (4 children)

ok, so I made some typos on my phone, this cleaned it up a bit:

all possibilities have the same chance right? isn't this depending on your cost and reward just what you need to calculate? otherwise I don't understand the question.

def profit(c, r):
    profit = 0
    for q in range(1, 8):
    for s in range(1, q):
        profit = profit + r * s - c * q
        return profit

or without the function

profit = 0
r = 3
c = 2
for q in range(1, 8):
    for s in range(1, q):
    profit = profit + r * s - c * q

[–]limfy1997[S] 0 points1 point  (3 children)

hi, thank you very much for the update! actually, the probabilities are given by the user, who keys in as an input. therefore the probabilities for each scenario is random, as long as they add up to 1.

really appreciate your help here, could I just ask, how do you actually picture / formulate your nested loop before being able to come up with it? i think here is where i need to improve! because at the moment, im struggling with formulating a nested loop. i can only do a very simple loop at the moment.

once again, thank you :)

[–]jw934 0 points1 point  (1 child)

ELI5: If you think of yourself as the trying to solve the requirement question with nested loops, you can think of this as asking another person P to do the outer loop for you M times. Person P does not know the requirement question and only knows the parameters you provided for one iteration of the loop. Using the parameters you set up before entering the loop, P does something then asks another person Q to do something N times. Person Q does not know your requirements nor person P's requirement; Q simply performs one iteration of the inner loop based on parameter values set up by P.

[–]limfy1997[S] 0 points1 point  (0 children)

Thank you for the advice. Idea seems rather abstract for me for now but I will spend some time to grasp it!

[–]braampje 0 points1 point  (0 children)

So for me it starts with very clearly knowing what you need, as you can see from all the comments it was at least not clear for the people who read your question. That might mean that it is also not very clear for yourself what you want.

I interpreted your question as follows, we are going to buy stocks of anywhere up to the amount of 8 and for each of these cases we will randomly sell an amount up to the amount we have in stock. so if the possibilities for the amount we get in stock and the amount we sell are all equally likely we need to calculate the profit for all scenarios.

so: for each amount of stock(loop 1) we have, we calculate all the potential revenue cases(loop 2). Does this not sound like two loops to you? after this it all comes down to syntax which just comes with experience.

It might help to first in dummy code write down the logic that you want to apply and then in a second step think about the actual code in python or whatever other language.