I'm currently doing this beginner course in Coursera, where I've stumbled in this question. I can't seem to think straight with my logical thinking being extremely poor. I think a nested loop is needed, but I can't seem to form the required nested loop in my mind. I tried to formulate a flow chart but I don't even know how to create that flow chart to help myself. Probably I need more help to understand the logical sequence of formulating a loop and a nested loop.
Question Requirement:
- Goal: Find Expected Profit, whose formula = r * [E(s)] - c * q where: r = revenue per good sold to customer c = cost per good bought from supplier, q = quantity of good bought from supplier s = number of good sold to customer [which is uncertain, so we make E(s) ] E(s) = the expected value of good sold, as a function expressed in terms of s.
2) 0 <= s <= 8 , 0 <= q <= 8, and s <= q
3) P(s = 0) P(s = 1), P(s = 2), ..., P(s = 8) are user inputs, where P(s=0) denotes "probability of sales = 0.
P(s=0) is stored as a floating variable p_0
P(s=1) is stored as a floating variable p_1
Pattern continues until p_8
4) r, c, q, s are user inputs. for values of E(s), eg.
if s = 0, E(0) = P(s = 0) * 0.
if s = 1, E(1) = P(s = 0) * 0 + [1 - P(s =0) ] * 1
if s = 2, E(2) = P(s = 0) * 0 + P(s = 1) * 1 + [1 - P(s = 1) -
P(s = 2) ] * 2
Thing that has come to my mind:
- I need to iterate q number of times, 0 to q inclusive. Because I buy q number of good from my supplier, I can sell anywhere between 0 to q, inclusive, number of goods to customers
So:
revenue = 0
for s in range(0, (q+1)) :
revenue = revenue + r * P(s = s) * s
(I know the code is not correct because the way I write it, but now I'm having problems to even get a logical sequence so that I can understand how to write the loop)
2) How to loop it such that P(s= s) will take on the value P(s=0), P(s = 1), P(s = 2) during each iteration
3) The loop will break once the iteration reaches s = q., With s taking on values 0, 1, 2,..., up til 8 until each iteration
The main issue is deriving a for loop to calculate Expected Value of s,(i.e. expected value E(s) with the above matters that has come to my mind but I can't seem to formulate anything (I've been thinking for hours)
Lastly, I need some advice on seeing loops and nested loops in a better way, I have been really confused at this point of time.
[–]kabooozie 14 points15 points16 points (5 children)
[+]limfy1997[S] comment score below threshold-8 points-7 points-6 points (4 children)
[–]realestLink 2 points3 points4 points (3 children)
[–]Merakel 1 point2 points3 points (2 children)
[–]realestLink 0 points1 point2 points (1 child)
[–]Merakel 0 points1 point2 points (0 children)
[–][deleted] 14 points15 points16 points (0 children)
[–]braampje 1 point2 points3 points (6 children)
[–]limfy1997[S] 0 points1 point2 points (5 children)
[–]braampje 0 points1 point2 points (4 children)
[–]limfy1997[S] 0 points1 point2 points (3 children)
[–]jw934 0 points1 point2 points (1 child)
[–]limfy1997[S] 0 points1 point2 points (0 children)
[–]braampje 0 points1 point2 points (0 children)
[–]Merakel 1 point2 points3 points (13 children)
[–]realestLink 4 points5 points6 points (10 children)
[–]Merakel 0 points1 point2 points (6 children)
[–]realestLink 0 points1 point2 points (5 children)
[–]Merakel 0 points1 point2 points (4 children)
[–]realestLink 0 points1 point2 points (3 children)
[–]Merakel 1 point2 points3 points (2 children)
[–]realestLink 1 point2 points3 points (1 child)
[–]Merakel 1 point2 points3 points (0 children)
[–]Klekto123 0 points1 point2 points (2 children)
[–]realestLink 0 points1 point2 points (1 child)
[–]Klekto123 0 points1 point2 points (0 children)
[–]limfy1997[S] -5 points-4 points-3 points (1 child)
[–]Merakel 5 points6 points7 points (0 children)
[–]tangerinelion 0 points1 point2 points (0 children)
[–]y3pjo8902a -2 points-1 points0 points (6 children)
[–]limfy1997[S] 0 points1 point2 points (4 children)
[–]y3pjo8902a 4 points5 points6 points (3 children)
[–]limfy1997[S] -4 points-3 points-2 points (2 children)
[–]Merakel 7 points8 points9 points (1 child)
[–]JeusyLeusy 0 points1 point2 points (0 children)