user3133346
user3133346

Reputation: 1

How can this loop work?

If I have a loop that depends on two variables, for example: while x is higher than 0 and y is higher than 0: do some stuff. but when x is equal or lower than 0 and y is still higher than 0, do stuff_X; and when x is still higher than 0 but y goes equal or lower than 0, do stuff_Y.

How can I write that in python language? I don't know how to use "or" and "and" in python, so I think they can be useful. I tried with "while" but I don't know how to end it.

while x > 0 (and?) y > 0:
    some
    stuff
    here
    if x <= 0:
        stuff_X
    if y <= 0:
        stuff_Y

Upvotes: 0

Views: 51

Answers (2)

user3126264
user3126264

Reputation:

(can't yet comment to add onto Christian's answer) Here's a tutorial that might help you understand and/or further:

Tutorial

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34146

You must use an or:

while x > 0 or y > 0:
    some
    stuff
    here
    if x <= 0:
        stuff_X
    if y <= 0:
        stuff_Y

It will end when both x and y are less or equal to 0.

Upvotes: 1

Related Questions