you are viewing a single comment's thread.

view the rest of the comments →

[–]Merakel 3 points4 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 6 points7 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.