if/Else statements and time.Leep()

Asked

Viewed 303 times

1

I have a question. It is possible to put the time.sleep() in a statement if/else?

Example:

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

I searched the internet but found no enlightening results.

1 answer

6

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

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

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

Exit:

None

Therefore, it cannot be used because the time.Sleep() method does not return value, what it could do (although it makes no sense is) :

#!/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.

  • So how can I get it to print something after 2 seconds? example: if #after 2 seconds: print "hello"

  • the time.sleep(2) already suspends the thread for you. No need to put in a conditional operation. Just put time.sleep(2) and print("Olá mundo")

  • Elif machine == "": import time if temp >=20: print "Starting Powerbit 1.0..." starting_so = time.Sleep(float(temp))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.