Ant232
Posts: 115
Joined: Thu Jul 12, 2012 9:34 pm

Quick time.sleep(????) question.

Sun Mar 23, 2014 1:47 pm

Hi,

Can you have a variable in the time.sleep() method? i.e

Blah = 8

time.sleep(Blah)

I have been trying this but it doesn't seem to work. It's saying that it should be a float. I tried assigning 20.0 to it but still no luck.

Any ideas?

Thanks in advance..
Ant

User avatar
DougieLawson
Posts: 39613
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Quick time.sleep(????) question.

Sun Mar 23, 2014 1:52 pm

Code: Select all

blah = 8.0
time.sleep(blah)
python numbers default to integers.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Ant232
Posts: 115
Joined: Thu Jul 12, 2012 9:34 pm

Re: Quick time.sleep(????) question.

Sun Mar 23, 2014 2:09 pm

Hi,

Wow, thanks for the quick reply..
It seems to be receiving a float. Maybe I should explain what's happening.

Web form sends a number to a txt file via PHP.
Python reads the text file and assigns a variable from the result.
The variable is the used in the time.sleep() method.

Code: Select all

f = open("/home/pi/webform/details.txt","r") 
imglen = (f.readline())

print imglen

Then I have this in a loop:

            myProc=subprocess.Popen( [ "fbi", "--once", "-autoup", "-T", "1", "-noverbose", "-readahead" , infile])
            time.sleep(imglen)
            myProc.kill()
            myProc.wait()
When it reaches the time.sleep(imglen) it crashes the script and tells me it should be a float.
I have the 'print imglen' there to verify that it has the correct number assigned to it. When I start the script it outputs a float.

Thanks
Ant

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: Quick time.sleep(????) question.

Sun Mar 23, 2014 2:18 pm

When printed it seems to be a float but in reality it is a string. You have to convert the string to a float using the built-in float() function.

Ant232
Posts: 115
Joined: Thu Jul 12, 2012 9:34 pm

Re: Quick time.sleep(????) question.

Sun Mar 23, 2014 2:37 pm

That's it. All working now. Many many thanks for the replies.

One question for future reference. How did you know it was a string? Does it become a string when Python reads the textfile?

Thanks again.

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

Re: Quick time.sleep(????) question.

Sun Mar 23, 2014 3:03 pm

Ant232 wrote: One question for future reference. How did you know it was a string? Does it become a string when Python reads the textfile?
The readline() method reads a line from a textfile and returns it as a string.

355/113
Posts: 26
Joined: Wed Aug 22, 2012 2:22 am

Re: Quick time.sleep(????) question.

Tue Mar 25, 2014 12:50 am

In case other people are as confused as I was at first on this issue, the answer to the op question "
Can you have a variable in the time.sleep() method? i.e
Blah = 8
time.sleep(Blah)
is *yes*, you can use float variables in time.sleep()
I am using one to drive a servo with RPIO pwm function and my oscilloscope shows me that it does work. I just mention this because I had just written code yesterday and then saw this posting. The op got into problems by reading a text file which did not supply a float, only a string version.
HTH

Return to “Python”