If / else statements and time.sleep ()

1

I have a question. Is it possible to set time.sleep() to a if/else statement?

Example:

import time
if time.sleep(2):
    print "hello"
else:
    #alguma coisa 

I searched the internet but found no illuminating results.

    
asked by anonymous 08.07.2014 / 17:13

1 answer

6

No, notice what happens if you run the following code:

#!/usr/local/bin/python2.7
import time

var = time.sleep(2)
print(var)

Output:

  

None

So you can not use it because the time.sleep () method does not return value, which could do (though it does not make sense):

#!/usr/local/bin/python2.7
import time

var = time.sleep(2)
print(var)
if var is None:
  print("Verdadeiro")
else:
  #nunca entrará aqui
  print("falso")

Although you read, python 2.7, this code is compatible with version 3.

    
08.07.2014 / 17:26