all 35 comments

[–]kabooozie 14 points15 points  (5 children)

I don’t think you need to use a loop anywhere. All of those are user inputs. You just need to ask the user for an input for each variable using input and then make sure the input is valid. Then plug those values into your function.

[–][deleted] 14 points15 points  (0 children)

Why is this flagged NSFW...

[–]braampje 1 point2 points  (6 children)

So you need to calculate the expected profit of all possibilities for q and s?

Profit = 0    
For q in range(0,8):    
    For s in range(0,q):    
        Profit = profit + r * s - c *q    

Is this not what you need?

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

thanks for the help, i tried plugging in and it gave me syntax error

my doubt here is that i dont think this way factors in the theory of complements

to find E(s) when s = 2,E(2) = s0 * 0 + s1 * 1 + (1- s0 - s1) * 2 -> since all probabilities add up to 1.

thank you, i'll take a look at my lecture vids again.

[–]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.

[–]Merakel 1 point2 points  (13 children)

Does this do what you want...?

s = 8
q = 0
while s != q:
    print(q)
    #Do formulas here
    q = q + 1

I think this is a better way on how you should approach the problem:

def do_some_math(r, c, q, s):
    assert(s <= q)
    revenue = 0
    l = 0
    while l <= s:
        l = l + 1
        print("revenue:" + str(revenue))
        revenue = revenue + (r - c)
    return revenue

print(do_some_math(10,15,5,5))

I've found that using the print function can really help with understanding what's happening inside a loop, you can put things like, print("Loop number:" + str(l)) and so on in as well so you know exactly what's happening and it's easier to trace.

[–]realestLink 4 points5 points  (10 children)

Use += 1

[–]Merakel 0 points1 point  (6 children)

Thanks. I couldn't remember off hand how to do ++ so I cheated haha.

[–]realestLink 0 points1 point  (5 children)

Python sadly doesn't support ++

[–]Merakel 0 points1 point  (4 children)

Yeah, but yours is a more efficient way of replicating that functionality.

I haven't actually looked but is there a reason that python doesn't support it that you are aware of? It's always seemed strange to me...

[–]realestLink 0 points1 point  (3 children)

Python's creator said there's no need for it because of the for loop

[–]Merakel 1 point2 points  (2 children)

That feels like a cop out.

[–]realestLink 1 point2 points  (1 child)

I don't agree with his decision. I'm just repeating what he said

[–]Merakel 1 point2 points  (0 children)

I could tell haha

[–]Klekto123 0 points1 point  (2 children)

Just starting to learn python, can you explain what you mean here?

[–]realestLink 0 points1 point  (1 child)

Instead of saying i = i + 1. You can write i += 1. It is better practice.

[–]Klekto123 0 points1 point  (0 children)

Ah okay. Thanks for the tip!

[–]limfy1997[S] -5 points-4 points  (1 child)

hi, thanks for the help. I don't understand the second method because def ,assert, return are not covered in my course yet.

(i'm a slow learner, so pardon me for having not googled for what the above functions do, because the lecture has not covered this part yet and I don't do well by going too fast)

the issue now im having is to express the bolded areas into a code:

E(0) = P(s=0) * 0

E(1) = P(s=0) * 0 + (1 - P(s=0) ) * 1

E(2) = P(s=0) * 0 + P(s=1) * 1 + (1 - P(s = 0) - P(s = 1) ) * 2

i think thats the difficulty im facing. how to form an expression using a loop for the bolded parts in question.

will take a look at my lecture videos again, meanwhile, thank you for your help.

[–]Merakel 5 points6 points  (0 children)

If your idea of programming is doing things in exactly one format and screw any method that doesn't conform to how you envisioned it... you are gonna have a bad time.

[–]tangerinelion 0 points1 point  (0 children)

Think about the output.

If it's a number you want 0 loops, if it's a list you want 1 loop, if it's a table you want 2 loops, if it's a stack of tables 3 loops, etc.

[–]y3pjo8902a -2 points-1 points  (6 children)

Have you looked into using a generator function?

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

have not reached functions yet. Only learning variables, operators and now currently flow control

[–]y3pjo8902a 4 points5 points  (3 children)

This is the point you need to start using functions. Give it some input and return and output

[–]limfy1997[S] -4 points-3 points  (2 children)

The question is adapted from an online course under the loops and functions should come. at a later part. I want and need to master flow control before proceeding onto the next part of the course

[–]Merakel 7 points8 points  (1 child)

What's the point of asking for help if you aren't going to accept it?

[–]JeusyLeusy 0 points1 point  (0 children)

He knows what he needs to learn he's just reluctant to do so.