tags:

views:

73

answers:

4

hi all,

is there any way to make this logic:

I need to make a statement one time only if the condition is false as below:

while 1:
    statement1
    statement2
    if condition:                --condition is true here
          statement3
    else                         --condition is false here
          statement3            --I need to do this "statement3" one time only
    if another condition:
          break

what I mean that I need to send my data if speed > 3 else send my data only one time.

any help, please

I solved it. I just need to add extra "neverdone = True" to the solution of "Alex Martelli"

 neverdone = True
 while 1:
    statement1
    statement2
    if condition:
         statement3
         neverdone = True
    elif neverdone:
         neverdone = False
         statement3
    if anothercondition:
        break

many thanks to Alex Martelli.

A: 

Couldn't you just put a break statement after statement3? That would mean that it would run once and then the while loop would exit.

avpx
I don't want to exit from my while loop.
mahdi86
+3  A: 

add a boolean variable:

neverdone = True
while 1:
    statement1
    statement2
    if condition:
          statement3
    elif neverdone:
          neverdone = False
          statement3
    if anothercondition:
          break
Alex Martelli
I don't want it to be only once in the while loop. I want to make it once just if the condition is false.
mahdi86
@mahdi, exactly: so I'm checking and setting `neverdone` only if `condition` is false (that's when `elif` executes -- it's a Python keyword used instead of `else if` -- when `condition` is true, the `if condition` branch executes and the `elif` branch is completely skipped!).
Alex Martelli
I didn't mean all waht you think, my problem is that I want to send my data whenever the speed > 3, but when the speed < 3 then I need to send my data only one time and then stop sending it until the speed > 3 happens again I will do the same procedure again.
mahdi86
@mahdi, and why do you think this code won't do **exactly** what you're describing? If `condition` is `speed > 3`, and is false, this code will do `statement3` exactly once (whether that statement is `sending` or whatever), but start doing it again if in the rest of the loop something changes `speed` so that `speed > 3` becomes true. Maybe you need to edit your question with less-absurd abstractions for `speed > 3` and `sending`...?!
Alex Martelli
what you are saying here is not doing my job. when "neverdone" will be true again?
mahdi86
@mahdi, you said you wanted to execute the statement **once** if `condition` is false (there's even a "one time only" in the **title** of the Q, for goodness sake!): that's what my code does (it will execute the statement **once** if `condition` is false). If what you write is not what you **mean**, just set `neverdone=True` again whenever it is you want to make believe the "once" or "one time only" doesn't apply anymore (e.g. at end of loop, or when `condition` is true, or whenever -- but **say what you mean**, and DON'T say "one time only" if you **don't** mean ONE time only).
Alex Martelli
A: 

you can use a flag, or write something like:

while 1:
  state1()
  state2()
  state3()
  if not condition:
     state3 = lambda : None
  if another_condition:
     break

Thise will destroy the state3 when the condition is false.

Oren
A: 

If you want to do something once don't put it in a infinite loop (duh)

def dostuff():
    statement1
    statement2
    if condition:
        statement3

dostuff()

if not contition:
    statement3

while True:
    dostuff()
    if another condition:
        break
THC4k